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