First, bitmap turn drawable
Bitmap bm=xxx; XXX based on your situation to obtain
Bitmapdrawable bd=new bitmapdrawable (BM);
Because Btimapdrawable is a subclass of drawable, it is finally possible to use the BD object directly.
Second, drawable turn bitmap
After turning into an bitmap object, the Drawable object can be stored in a byte output stream via the SK-O-Android, and eventually it can be saved as JPG and PNG files.
Drawable d=xxx; XXX gets drawable According to his own situation
bitmapdrawable BD = (bitmapdrawable) D;
Bitmap BM = Bd.getbitmap ();
Finally, BM is the bitmap object we need.
Get bitmap from your resources
public static Bitmap getbitmapfromresources (Activity Act, int resId) {
Resources res = act.getresources ();
Return Bitmapfactory.decoderesource (res, resId);
}
Byte[]→bitmap
public static Bitmap Convertbytes2bimap (byte[] b) {
if (B.length = = 0) {
return null;
}
Return Bitmapfactory.decodebytearray (b, 0, b.length);
}
Bitmap→byte[]
public static byte[] Convertbitmap2bytes (Bitmap BM) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
return Baos.tobytearray ();
}
1) Drawable→bitmap
public static Bitmap Convertdrawable2bitmapbycanvas (drawable drawable) {
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);
Canvas.setbitmap (bitmap);
Drawable.setbounds (0, 0, drawable.getintrinsicwidth (),
Drawable.getintrinsicheight ());
Drawable.draw (canvas);
return bitmap;
}
2) Drawable→bitmap
public static Bitmap Convertdrawable2bitmapsimple (drawable drawable) {
bitmapdrawable BD = (bitmapdrawable) drawable;
return Bd.getbitmap ();
}
Bitmap→drawable
public static drawable convertbitmap2drawable (Bitmap Bitmap) {
bitmapdrawable bd = new bitmapdrawable (bitmap);
Because Btimapdrawable is a subclass of drawable, it is finally possible to use the BD object directly.
return BD;
}
Drawable and Bitmap conversions