Drawable and Bitmap conversions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.