How to convert a View into a Bitmap
Method 1:
/*** Draw the View to Bitmap * @ param view the View to be drawn * @ param width the width of the View * @ param height the height of the View * @ return returns the Bitmap object * add by csj 13-11-6 */public Bitmap getViewBitmap (View comBitmap, int width, int height) {Bitmap bitmap = null; if (comBitmap! = Null) {comBitmap. clearFocus (); comBitmap. setPressed (false); boolean willNotCache = comBitmap. willNotCacheDrawing (); comBitmap. setWillNotCacheDrawing (false); // Reset the drawing cache background color to fully transparent // for the duration of this operationint color = comBitmap. getDrawingCacheBackgroundColor (); comBitmap. setDrawingCacheBackgroundColor (0); float alpha = comBitmap. getAlpha (); comBitmap. s EtAlpha (1.0f); if (color! = 0) {comBitmap. destroyDrawingCache ();} int widthSpec = View. measureSpec. makeMeasureSpec (width, View. measureSpec. EXACTLY); int heightSpec = View. measureSpec. makeMeasureSpec (height, View. measureSpec. EXACTLY); comBitmap. measure (widthSpec, heightSpec); comBitmap. layout (0, 0, width, height); comBitmap. buildDrawingCache (); Bitmap cacheBitmap = comBitmap. getDrawingCache (); if (cacheBitmap = null) {Log. e ("view. processImageToBlur "," failed getViewBitmap ("+ comBitmap +") ", new RuntimeException (); return null;} bitmap = Bitmap. createBitmap (cacheBitmap); // Restore the viewcomBitmap. setAlpha (alpha); comBitmap. destroyDrawingCache (); comBitmap. setWillNotCacheDrawing (willNotCache); comBitmap. setDrawingCacheBackgroundColor (color);} return bitmap ;}
Method 2:
private Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e("Folder", "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
Method 3:
View view = mLauncher.getDragLayer();view.setDrawingCacheEnabled(true);Bitmap bitmap = view.getDrawingCache();