Practical tips for Android development: Secrets to be said between Drawable and bitmap

Source: Internet
Author: User

Bitmap-called bitmaps, the general bitmap file format suffix bmp, of course, there are many encoders such as RGB565, RGB888. As a per-pixel display object, the execution efficiency is high, but the disadvantage is obvious that the storage efficiency is low. We understand that it's better to have a storage object.


drawable-as a generic graphic object for Android, it can load images in commonly used formats, such as GIF, PNG, JPG, and of course, BMP, and of course some advanced visualizations, such as gradients, graphics, etc.
I. Bitmap Turn drawable
Bitmap bm = XXX;//xxx Get it according to your situation

Bitmapdrawable bd=new bitmapdrawable (BM);
Because Btimapdrawable is a subclass of drawable, it is finally possible to use the BD object directly.

two. 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.

three. Get bitmap from your resources
Public static Bitmap getbitmapfromresources (Activity Act, int resId) {
Resources res = act.getresources ();
Return Bitmapfactory.decoderesource (res, resId);
}


four. byte[] Turn bitmap
public static Bitmap Convertbytes2bimap (byte[] b) {
if (B.length = = 0) {
return null;
}
Return Bitmapfactory.decodebytearray (b, 0, b.length);
}

Five. Bitmap turn byte[]
Public Static byte[] Convertbitmap2bytes (Bitmap BM) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
return Baos.tobytearray ();
}


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.