How to convert a View into a Bitmap

Source: Internet
Author: User

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





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.