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操作相关工具类
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.