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();