Julia
Julia

Reputation: 1399

Android screenshot of a layout to bitmap

I'm trying to take a screenshot of a relative layout which consists of two imageview. One of imageview is able to move when on touch.

Here's how the code looks like

 <RelativeLayout
 android:id="@+id/imageLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
    android:id="@+id/imgPhoto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
   />

<ImageView
    android:id="@+id/imgBackground"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:layout_centerVertical="true"
    />
 </RelativeLayout>

Following is the code that I have for creating a bitmap of the layout.

        imageLayout.setDrawingCacheEnabled(true);
        imageLayout.buildDrawingCache();
        Bitmap mergedImage = imageLayout.getDrawingCache();

The code works fine, and it is able to take a screen shot of the two images. However, when I move the imgPhoto around, the screen shot doesn't reflect the changed position. It still shows the original unmoved imgPhoto. How can I fix that? Thanks.

Upvotes: 0

Views: 96

Answers (1)

ezaquarii
ezaquarii

Reputation: 1916

Try disabling the drawing cache before you try again:

imageLayout.setDrawingCacheEnabled(true);
imageLayout.buildDrawingCache();
Bitmap mergedImage = imageLayout.getDrawingCache();
imageLayout.setDrawingCacheEnabled(false);

That should work.

You can also use View.destroyDrawingCache().

Upvotes: 1

Related Questions