Android:BitMap操作関連ツール類
4090 ワード
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;
}
}