Android: oversized bitmap

DESCRIPTION OF THE PROBLEM:
I checked the following two methods to create a snapshot of the bitmap from the view (e.g. RelativeLayout in this case). Both methods are great for presentations whose dimensions (for example, width and height) are smaller than the screen sizes of the device (for example, 480x900). The visible part of the view is captured and written to the bitmap. Unfortunately, the bitmap is black in the invisible part of the view.

QUESTION:
How can I capture also the invisible part of the view?

CODE:

public class NewRelativeLayout extends RelativeLayout{ private Bitmap bmp; public NewRelativeLayout(Context context){ super(context); this.setLayoutParams(new RelativeLayout.LayoutParams(2000,2000)); // add here methods to configure the RelativeLayout or to add children } //This is the first method public void makeSnapshot_Method1(){ this.setDrawingCacheEnabled(true); bmp = Bitmap.createBitmap(this.getDrawingCache()); this.setDrawingCacheEnabled(false); } //This is the second method public void makeSnapshot_Method2(){ bmp = Bitmap.createBitmap(2000, 2000, Bitmap.Config.ARGB_8888); Canvas helpCanvas = new Canvas(bmp); this.draw(helpCanvas); } 

}

+6
source share
3 answers

I also searched for this solution and, in the end, I was able to come up with my own solution, which is actually quite simple.

In my case, I had a LinearLayout that contained several View elements, some of which sometimes were not on the screen, vertically below the end of the borders of the screen. I was able to save the View as a bitmap (see below loadBitmapFromView() ), but had problems when it expanded beyond the bottom of the screen.

My solution was to make part of the LinearLayout of the LinearLayout composite.

eg.

It:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/my_linear_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- Some Views added here at runtime --> </LinearLayout> 

Became:

Pay attention to using wrap_content to ensure ScrollView is scaled to the height of your content.

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scroll" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/my_linear_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <!-- Some Views added here at runtime --> </LinearLayout> </ScrollView> 

It seemed that in order for me to save the View as a bitmap, it was possible to manually lay out elements off the screen. I used the following method to save a bitmap. I found this a lot earlier on SO, but I can't find the question to edit it correctly (whoever you are - thanks!).

For the next method, I pass the link to LinearLayout above ( my_linear_layout ).

 public static Bitmap loadBitmapFromView(View view) { Bitmap bitmap = null; // width measure spec int widthSpec = View.MeasureSpec.makeMeasureSpec( view.getMeasuredWidth(), View.MeasureSpec.AT_MOST); // height measure spec int heightSpec = View.MeasureSpec.makeMeasureSpec( view.getMeasuredHeight(), View.MeasureSpec.AT_MOST); // measure the view view.measure(widthSpec, heightSpec); // set the layout sizes int left = view.getLeft(); int top = view.getTop(); int width = view.getMeasuredWidth(); int height = view.getMeasuredHeight(); int scrollX = view.getScrollX(); int scrollY = view.getScrollY(); view.layout(left, top, width + left, height + top); // create the bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); // create a canvas used to get the view image and draw it on the // bitmap Canvas c = new Canvas(bitmap); // position the image inside the canvas c.translate(-view.getScrollX(), -view.getScrollY()); // get the canvas view.draw(c); return bitmap; } 
+1
source

Depends on which image you want to capture, an image from a background image or foreground. In any case, you need to get the Drawable from them, drop them in BitmapDrawable, and get the Bitmap from them. The code: -

 BitmapDrawable bd=(BitmapDrawable) view.getBackground(); //if you need background image BitmapDrawable bd=(BitmapDrawable) view.getDrawable(); //if you need foreground image (Only available to some views who have an android:src xml property) Bitmap bm=bd.getBitmap(); 

I hope this works for you.

0
source

I am also trying to figure this out for a ListView to get all the rows that are not on the screen.

I found many things that I would like to share:

Here, someone asks if a bitmap can be created from an unreadable view: Bitmap from View doesn't display on Android

Here they claim that it is a duplicate, and it also answered correctly (?) It contains a URL with some encoding, which is old and not compiled. Also a possible duplicate question did not help me. Android will get full view as a bitmap

I think I'll try to do this using this method: Viewing a list using a bitmap I already came up with the same idea, now I still need to figure out how to do it.

I hope you can use some of this information.

0
source

Source: https://habr.com/ru/post/908528/


All Articles