Converts a view object to a bitmap object.

Source: Internet
Author: User

It is feasible and tested in person

Turn: http://blog.csdn.net/hexingzhi/article/details/7598567

1. Bitmap to drawable bitmap Bm = xxx; // XXX obtain bitmapdrawable BD = new bitmapdrawable (BM) based on your situation, because btimapdrawable is a subclass of drawable, you can directly use the BD object. 2. After drawable is converted into a bitmap object through bitmap, The drawable object can be converted into a byte output stream through the android SK inventory, and finally saved as JPG and PNG files. Drawable d = xxx; // XXX obtain drawable bitmapdrawable BD = (bitmapdrawable) D according to your own situation; bitmap Bm = BD. getbitmap (); finally, BM is the bitmap object we need. // Obtain bitmap 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 (bit Map BM) {bytearrayoutputstream baos = new bytearrayoutputstream (); BM. compress (bitmap. compressformat. PNG, 100, baos); Return baos. tobytearray ();} // 1) drawable → bitmap public static bitmap convertdrawable2bitmapbycanvas (drawable) {Bitmap bitmap = bitmap. createbitmap (drawable. getintrinsicwidth (), drawable. getintrinsicheight (), drawable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888: bitmap. config. rgb_565); 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) {bitmapdrawable BD = (bitmapdrawable) drawable; return BD. getbitma P () ;}// bitmap → drawable public static drawable convertbitmap2drawable (Bitmap bitmap) {bitmapdrawable BD = new bitmapdrawable (Bitmap); // btimapdrawable is a subclass of drawable, you can directly use the BD object. Return BD ;}

 

Private bitmap getviewbitmap (view ){

View. clearfocus ();
View. setpressed (false );

Boolean willnotcache = view. willnotcachedrawing ();
View. setwillnotcachedrawing (false );

Int color = view. getdrawingcachebackgroundcolor ();
View. setdrawingcachebackgroundcolor (0 );

If (color! = 0 ){
View. destroydrawingcache ();
}
View. builddrawingcache ();
Bitmap cachebitmap = view. getdrawingcache ();
If (cachebitmap = NULL ){
Return NULL;
}

Bitmap bitmap = bitmap. createbitmap (cachebitmap );

View. destroydrawingcache ();
View. setwillnotcachedrawing (willnotcache );
View. setdrawingcachebackgroundcolor (color );

Return bitmap;
}

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.