How to change the content on the android screen, and combine two bitmaps to display the image, androidbitmap
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:
<Span style = "white-space: pre"> </span> // This is the length I have defined from the resource file and can be set freely.
<span style="white-space:pre">float dimension = getResources().getDimension(<span style="white-space:pre"></span>R.dimen.activity_action_bar_height);</span>
</pre><pre name="code" class="java">
<Span style = "white-space: pre"> </span> <pre name = "code" class = "java" style = "font-size: 13.333331041162px; "> <span style =" white-space: pre "> </span> // the View that you want to add is a previously written View used to generate a replaced bitmap <span> </span> conView. setDrawingCacheEnabled (true); <span> </span> conView. buildDrawingCache (); <span> </span> Bitmap bitmap3 = conView. getDrawingCache (); // This method requires attention. Next we will talk about <span> </span> Bitmap bitmap2 = Bitmap. createBitmap (bitmap3, 0, statusBarHeight, <span> </span> 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 );
<span style="white-space:pre"></span>Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width,height - statusBarHeight - height / 3 + 100);//
<Span style = "white-space: pre"> </span> // Replace the generated bitmap with position B in the original bitmap. setPixels (pixels, 0, w, width/5, 0, w, h );
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:
<span style="line-height: 19.5px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"><span style="line-height: 1.5;"></span></span><pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px; line-height: 18px; font-family: 'Courier New' !important;"><span style="color: rgb(0, 0, 255); line-height: 1.5 !important; font-size: 12px !important;">if</span> (width <= 0 || height <= 0 ||(width * height * (opaque && !translucentWindow ? 2 : 4) ><span style="line-height: 1.5 !important; font-size: 12px !important;"> ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) { destroyDrawingCache(); </span><span style="color: rgb(0, 0, 255); line-height: 1.5 !important; font-size: 12px !important;">return</span><span style="line-height: 1.5 !important; font-size: 12px !important;">; } </span>
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;
}
<span style="line-height: 19.5px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"><span style="line-height: 1.5;"></span></span>
<Span style = "line-height: 19.5px; color: rgb (75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; "> <span style =" line-height: 1.5; "> in this way, you can use this method to replace the bitmap generated by View. </span>
In android, how does one combine two bitmaps into one bitmap and display them up and down?
PublicBitmap toConformBitmap (Bitmap background, Bitmap foreground) {if (background = null) {return null;} int bgWidth = background. getWidth (); int bgHeight = background. getHeight (); // int fgWidth = foreground. getWidth (); // int fgHeight = foreground. getHeight (); // create the new blank bitmap to create a new Bitmap with the same length and width as SRC newbmp = Bitmap. createBitmap (bgWidth, bgHeight, Config. ARGB_8888); Canvas cv = new Canvas (newbmp); // draw bg into cv. drawBitmap (background, 0, 0, null); // draw bg at coordinates 0, 0 // draw fg into cv. drawBitmap (foreground, 0, 0, null); // you can draw fg at coordinates 0, 0, and save all clip cv. save (Canvas. ALL_SAVE_FLAG); // save // store cv. restore (); // store return newbmp ;}
How to Use setPixels to combine two bitmaps into a bitmap ??
Using for loop, setpixels (large image) is not covered in large images, and pixels of small images are set elsewhere