How to change the screenshot content during android screenshot capture and combine two bitmaps to display the image

Source: Internet
Author: User

How to change the content on the android screen and combine two bitmaps to display the image

The requirements in the project are as follows:

This method is used to input a desired activity.

/*** @ Param activity * @ return */public Bitmap takeScreenShot (activity Activity) {View rootView = activity. getWindow (). getDecorView (); rootView. setDrawingCacheEnabled (true); rootView. buildDrawingCache (); Bitmap bitmap = rootView. getDrawingCache (); // get the Rect frame of the status bar = new Rect (); activity. getWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame); int statusBarHeight = frame. top; // Log. I ("TAG", "" + statusBarHeight); // get the screen length and height int width = activity. getWindowManager (). getdefadisplay display (). getWidth (); int height = activity. getWindowManager (). getdefadisplay display (). getHeight (); // remove the title bar Bitmap B = Bitmap. createBitmap (bitmap, 0, statusBarHeight, width, height-statusBarHeight-height/3 + 100); rootView. setDrawingCacheEnabled (false); return B ;}

However, I found that some of the captured image content needs to be modified. For example, if I want to replace the title with another one, I can only create a bitmap if the original activity cannot be modified, and add the bitmap to the corresponding location of the bitmap returned above. The method is as follows:

// This is the length defined in the resource file and can be set freely.
float dimension = getResources().getDimension(R.dimen.activity_action_bar_height);
 
 
// ConView: the View that I previously wrote is used to generate the replaced bitmapconView. setDrawingCacheEnabled (true); conView. buildDrawingCache (); Bitmap bitmap3 = conView. getDrawingCache (); // This method requires attention. Bitmap bitmap2 = Bitmap will be described below. createBitmap (bitmap3, 0, statusBarHeight, width, (int) dimension );
Int w = bitmap2.getWidth (); int h = bitmap2.getHeight (); int [] pixels = new int [w * h]; bitmap2.getPixels (pixels, 0, w, 0, 0, 0, w, h );
Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width,height - statusBarHeight - height / 3 + 100);//
// Replace the generated bitmap with position B. setPixels (pixels, 0, w, width/5, 0, w, h) in the original bitmap );


An error is reported when the running result is found. The bitmap generated by the custom View is null, that is, Bitmap bitmap3 = conView. getDrawingCache (); The returned value is null. It turns out that the bitmap for conView computing exceeds the system memory limit, mainly because the value of drawingCache is greater than the value given by the system. Let's take a look at a piece of code in the buildDrawingCache () method:

if (width <= 0 || height <= 0 ||(width * height * (opaque && !translucentWindow ? 2 : 4) > ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {                    destroyDrawingCache();                    return;     } 
In the code above, width and height are the width and height of the view to be cached, so (width * height * (opaque &&! TranslucentWindow? 2: 4) The current cache size is calculated. ViewConfiguration. get (mContext). getScaledMaximumDrawingCacheSize () obtains the maximum DrawingCache value provided by the system. When the maximum drawingCache value provided by the required DrawingCache> system is used, the Bitmap is generated and the obtained Bitmap is null. Therefore, you only need to modify the required cache value to solve the problem. So we introduce the second method:
Public static Bitmap convertViewToBitmap (View view ){
View. measure (MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED), MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED ));
View. layout (0, 0, view. getMeasuredWidth (), view. getMeasuredHeight ());
View. buildDrawingCache ();
Bitmap bitmap = view. getDrawingCache ();


Return bitmap;
}


In this way, you can replace the bitmap generated by View with this method.


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.