Android Drawable、Bitmap、byte[]之间
2018-05-21 本文已影响0人
WotYang
1. byte[ ] 转 Bitmap
BitmapFactory.decodeByteArray(byte, 0, byte.length)
2. Bitmap 转 byte[ ]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
3. Bitmap 转 Drawable
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
4. Drawable 转 Bitmap
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof NinePatchDrawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
} else {
return null;
}