public class AdjustBitmap {
/**
? ?? * 將bitmap調(diào)整到指定大小
? ?? */
? ? public static BitmapsizeBitmap(Bitmap origin, int newWidth, int newHeight) {
if (origin ==null) {
return null;
? ? ? ? }
int height = origin.getHeight();
? ? ? ? int width = origin.getWidth();
? ? ? ? float scaleWidth = ((float) newWidth) / width;
? ? ? ? float scaleHeight = ((float) newHeight) / height;
? ? ? ? Matrix matrix =new Matrix();
? ? ? ? // 使用后乘
? ? ? ? matrix.postScale(scaleWidth, scaleHeight);
? ? ? ? Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
? ? ? ? if (!origin.isRecycled()) {
//這時候origin還有嗎档插?
? ? ? ? ? ? origin.recycle();
? ? ? ? }
return newBM;
? ? }
/**
*
? ?? * @param origin
? ?? * @param scale
? ?? * @return 按比例縮放
? ?? */
? ? public static BitmapscaleBitmap(Bitmap origin, float scale) {
if (origin ==null) {
return null;
? ? ? ? }
int width = origin.getWidth();
? ? ? ? int height = origin.getWidth();
? ? ? ? Matrix matrix =new Matrix();
? ? ? ? matrix.preScale(scale, scale);
? ? ? ? Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
? ? ? ? if (newBM.equals(origin)) {
return newBM;
? ? ? ? }
origin.recycle();
? ? ? ? return newBM;
? ? }
/**
*
? ?? * @param bitmap
? ?? * @return 從中間截取一個正方形
? ?? */
? ? public static BitmapcropBitmap(Bitmap bitmap) {
// 得到圖片的寬,高
? ? ? ? int w = bitmap.getWidth();
? ? ? ? int h = bitmap.getHeight();
? ? ? ? // 裁切后所取的正方形區(qū)域邊長
? ? ? ? int cropWidth = w >= h ? h : w;
? ? ? ? return Bitmap.createBitmap(bitmap, 0,
? ? ? ? ? ? ? ? 0, cropWidth, cropWidth);
? ? }
/**
*
? ?? * @param bitmap
? ?? * @param sx 2
? ?? * @param sy 3
? ?? * @return 返回一個截取的圖形
? ?? */
? ? public static BitmapcropBitmap(Bitmap bitmap,int sx,int sy) {
// 得到圖片的寬,高
? ? ? ? int w = bitmap.getWidth();//600 900
? ? ? ? int h = bitmap.getHeight();//900 600
? ? ? ? int startWidth=0;
? ? ? ? int startHeight=0;
? ? ? ? int x=0;
? ? ? ? int y=0;
? ? ? ? int z=0;
? ? ? ? // 裁切后所取的正方形區(qū)域邊長
? ? ? ? if (sx==sy) {
x = w >= h ? h : w;
? ? ? ? ? ? y=x;
? ? ? ? }else if (sx>sy){
//橫 長方型
? ? ? ? ? ? if (w>h){//331:240 3:2
? ? ? ? ? ? ?? z=h/sx;
? ? ? ? ? ? }else {//240:331? 3:2
? ? ? ? ? ? ? ? z=x/sx;
? ? ? ? ? ? }
x=sx*z;
? ? ? ? ? ? y=sy*z;
? ? ? ? }else {
//豎型 長方型
? ? ? ? ? ? if (w>h){//331:240 2:3
? ? ? ? ? ? ? ? z=h/sy;
? ? ? ? ? ? }else {//240:331? 2:3
? ? ? ? ? ? ? ? z=x/sy;
? ? ? ? ? ? }
x=sx*z;
? ? ? ? ? ? y=sy*z;
? ? ? ? }
return Bitmap.createBitmap(bitmap, startWidth,
? ? ? ? ? ? ? ? startHeight, x, y);
? ? }
/**
*
? ?? * @param bitmap
? ?? * @return 把圖片裁剪成圓形
? ?? */
? ? public static BitmapgetCircleBitmap(Bitmap bitmap) {
if (bitmap ==null) {
return null;
? ? ? ? }
//裁剪成正方形
? ? ? ? bitmap =cropBitmap(bitmap);
? ? ? ? try {
Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(),
? ? ? ? ? ? ? ? ? ? bitmap.getHeight(), Bitmap.Config.ARGB_8888);
? ? ? ? ? ? Canvas canvas =new Canvas(circleBitmap);
? ? ? ? ? ? final Paint paint =new Paint();
? ? ? ? ? ? final Rect rect =new Rect(0, 0, bitmap.getWidth(),
? ? ? ? ? ? ? ? ? ? bitmap.getHeight());
? ? ? ? ? ? final RectF rectF =new RectF(new Rect(0, 0, bitmap.getWidth(),
? ? ? ? ? ? ? ? ? ? bitmap.getHeight()));
? ? ? ? ? ? float roundPx =0.0f;
? ? ? ? ? ? roundPx = bitmap.getWidth();
? ? ? ? ? ? paint.setAntiAlias(true);
? ? ? ? ? ? canvas.drawARGB(0, 0, 0, 0);
? ? ? ? ? ? paint.setColor(Color.WHITE);
? ? ? ? ? ? canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
? ? ? ? ? ? paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
? ? ? ? ? ? final Rect src =new Rect(0, 0, bitmap.getWidth(),
? ? ? ? ? ? ? ? ? ? bitmap.getHeight());
? ? ? ? ? ? canvas.drawBitmap(bitmap, src, rect, paint);
? ? ? ? ? ? return circleBitmap;
? ? ? ? }catch (Exception e) {
return bitmap;
? ? ? ? }
}
public static BitmapdrawableToBitmap(Drawable drawable) {
// 取 drawable 的長寬
? ? ? ? int w = drawable.getIntrinsicWidth();
? ? ? ? int h = drawable.getIntrinsicHeight();
? ? ? ? // 取 drawable 的顏色格式
? ? ? ? Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
? ? ? ? ? ? ? ? : Bitmap.Config.RGB_565;
? ? ? ? // 建立對應(yīng)bitmap
? ? ? ? Bitmap bitmap = Bitmap.createBitmap(w, h, config);
? ? ? ? // 建立對應(yīng) bitmap 的畫布
? ? ? ? Canvas canvas =new Canvas(bitmap);
? ? ? ? drawable.setBounds(0, 0, w, h);
? ? ? ? // 把 drawable 內(nèi)容畫到畫布中
? ? ? ? drawable.draw(canvas);
? ? ? ? return bitmap;
? ? }
}