package com.example.customalarm.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.WindowManager;
/**
* CustomUtils工具類
*
* @author BiHaidong
* @date 2015-11-11
*/
public class CustomUtils {
/**
* 取得屏幕參數(寬高)
*
* @param context
* 上下文對象
* @param options
* BitMap的Options對象
*
* @return Object[] {寬,高}
*
* @author BiHaidong
* @date 2015-11-11
*/
@SuppressLint("ServiceCast")
public static Object[] getScreenParamater(Context context) {
DisplayMetrics dm = new DisplayMetrics();
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
manager.getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
return new Object[] { screenWidth, screenHeight };
}
/**
* 根據屏幕大小取得壓縮比
*
* @param context
* 上下文對象
* @param options
* BitMap的Options對象
*
* @author BiHaidong
* @date 2015-11-11
*/
@SuppressLint("ServiceCast")
public static int getInSampleSize(Context context, Options options) {
DisplayMetrics dm = new DisplayMetrics();
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
manager.getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
int result = 1;
if (screenWidth != 0
&& screenHeight != 0
&& (options.outWidth > screenWidth || options.outHeight > screenHeight)) {
result = options.outWidth / screenWidth > options.outHeight
/ screenHeight ? options.outWidth / screenWidth
: options.outHeight / screenHeight;
}
return result;
}
/**
* 根據資源文件ID 取得壓縮的BitMapDrawable
*
* @param context
* 上下文對象
* @param id
* 資源文件ID
* @param size
* 壓縮比例 (100不壓縮)
*
* @author BiHaidong
* @date 2015-11-11
*/
public static BitmapDrawable getBitMapDrawable(Context context, int id,
int size) {
Bitmap bitmap = CustomUtils.getBitMap(context, id, size);
return new BitmapDrawable(context.getResources(), bitmap);
}
/**
* 根據資源文件ID 取得壓縮的BitMap
*
* @param context
* 上下文對象
* @param id
* 資源文件ID
* @param size
* 壓縮比例 (100不壓縮)
*
* @author BiHaidong
* @date 2015-11-11
*/
public static Bitmap getBitMap(Context context, int id, int size) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap welcomeBitmap = BitmapFactory.decodeResource(
context.getResources(), id, options);
int inSampleSize = CustomUtils.getInSampleSize(context, options);
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;// 設置縮放比例
// 重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了
welcomeBitmap = BitmapFactory.decodeResource(context.getResources(),
id, options);
Bitmap bitmap = compressImage(welcomeBitmap, size);
welcomeBitmap.recycle();
return bitmap;
}
/**
* 取得壓縮的BitMap
*
* @param image
* BitMap
* @param size
* 壓縮比例 (100不壓縮)
*
* @author BiHaidong
* @date 2015-11-11
*/
public static Bitmap compressImage(Bitmap image, int size) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 質量壓縮方法,這裏100表示不壓縮,把壓縮後的數據存放到baos中
image.compress(Bitmap.CompressFormat.JPEG, size, baos);
// 把壓縮後的數據baos存放到ByteArrayInputStream中
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
// 把ByteArrayInputStream數據生成圖片
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}
}
Android:BitMap操作相關工具類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.