package com.lcjian.library.util.common;
|
|
import android.content.res.Resources;
|
import android.graphics.Bitmap;
|
import android.graphics.Bitmap.Config;
|
import android.graphics.BitmapFactory;
|
import android.graphics.Canvas;
|
import android.graphics.Matrix;
|
import android.graphics.PixelFormat;
|
import android.graphics.drawable.Drawable;
|
import android.view.View;
|
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
|
/**
|
* ImageUtils
|
*
|
* @author LCJIAN
|
*/
|
public class DrawableUtils {
|
public static Bitmap drawableToBitmap(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;
|
// 建立对应 bitmap
|
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
|
// 建立对应 bitmap 的画布
|
Canvas canvas = new Canvas(bitmap);
|
drawable.setBounds(0, 0, w, h);
|
// 把 drawable 内容画到画布中
|
drawable.draw(canvas);
|
return bitmap;
|
}
|
}
|