Convert Bitmap objects

Source: Internet
Author: User

Convert a view to a bitmap object

1  Public StaticBitmap getviewbitmap (view view) {2View.measure (View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED),3View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED));4View.layout (0, 0, View.getmeasuredwidth (), View.getmeasuredheight ());5 View.builddrawingcache ();6Bitmap Bitmap =View.getdrawingcache ();7         returnbitmap;8}

Create a new bitmap based on the existing drawable:

1 PrivateBitmap Bitmap;2 Private voidDrawabletobitamp (drawable drawable)3     {4         intW =drawable.getintrinsicwidth ();5         inth =drawable.getintrinsicheight ();6System.out.println ("Drawable turn Bitmap");7Bitmap.config Config =8Drawable.getopacity ()! = Pixelformat.opaque?Bitmap.Config.ARGB_88889 : Bitmap.Config.RGB_565;TenBitmap =Bitmap.createbitmap (w,h,config); One         //Note that the following three lines of code to use, no in view or Surfaceview in the Canvas.drawbitmap will not see the diagram ACanvas Canvas =NewCanvas (bitmap);  -Drawable.setbounds (0, 0, W, h);  - Drawable.draw (canvas); the}

Remove the bitmap directly from the existing drawable:

1 Private Bitmap Bitmap; 2 3 Private void Drawabletobitamp (drawable drawable) 4     {5         bitmapdrawable bd = (bitmapdrawable) drawable; 6         Bitmap = bd.getbitmap (); 7     }

Bitmap to Byte

1 Private byte [] bitmap2bytes (Bitmap BM) {23     New Bytearrayoutputstream (); 4 5     Bm.compress (Bitmap.CompressFormat.PNG,BAOs); 6 7     return Baos.tobytearray ();   8  }

Byte[] Turn bitmap:

1 PrivateBitmap Bytes2bimap (byte[] b) {2 3                     if(b.length!=0){4 5                             returnBitmapfactory.decodebytearray (b, 0, b.length);6 7                     }8 9                     Else {Ten  One                             return NULL; A  -                     } -  the}

Convert Bitmap objects

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.