Converting Android View to Bitmap

Source: Internet
Author: User

Converting Android View to Bitmap
The parameter Canvas of the onDraw method in the View class is the background of View painting. to convert a View to a Bitmap, the painting operation on the Canvas is actually drawn to the Bitmap. The process of converting a View to a Bitmap is also called a screenshot. The methods in the View class involved in converting View to Bitmap include:

   protected void onDraw(Canvas canvas)   public void buildDrawingCache()   public void destroyDrawingCache()   public Bitmap getDrawingCache()   public void setDrawingCacheEnabled(boolean enabled)

 

The following are examples of common View screenshots: 1. View-to-Bitmap
public final Bitmap screenShot(View view) {        if (null == view) {            throw new IllegalArgumentException("parameter can't be null.");        }        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());        view.setDrawingCacheEnabled(true);        view.buildDrawingCache();        Bitmap bitmap = view.getDrawingCache();        return bitmap;    }  

 

2. The Activity is converted to Bitmap without the status bar.
public final Bitmap screenShot(Activity activity) {        if (null == activity) {            throw new IllegalArgumentException("parameter can't be null.");        }        View view = activity.getWindow().getDecorView();        view.setDrawingCacheEnabled(true);        view.buildDrawingCache();        Bitmap b1 = view.getDrawingCache();        Rect frame = new Rect();        view.getWindowVisibleDisplayFrame(frame);        int statusBarHeight = frame.top;        Point point = new Point();        activity.getWindowManager().getDefaultDisplay().getSize(point);        int width = point.x;        int height = point.y;        Bitmap b2 = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);        view.destroyDrawingCache();        return b2;    }

 

3. ScrollView to long Bitmap (similar to the screenshot of a hammer tab)
 public final Bitmap screenShot(ScrollView scrollView) {        if (null == scrollView) {            throw new IllegalArgumentException("parameter can't be null.");        }        int height = 0;        Bitmap bitmap;        for (int i = 0, s = scrollView.getChildCount(); i < s; i++) {            height += scrollView.getChildAt(i).getHeight();            scrollView.getChildAt(i).setBackgroundResource(android.R.drawable.screen_background_light);        }        bitmap = Bitmap.createBitmap(scrollView.getWidth(), height, Bitmap.Config.ARGB_8888);        final Canvas canvas = new Canvas(bitmap);        scrollView.draw(canvas);        return bitmap;    }

 

 

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.