0

I need to capture View to a Bitmap. That's how I tried to do that:

private Bitmap getBitmapFromView1(View rootView) {
    Bitmap b = Bitmap.createBitmap(rootView.getWidth(),
        rootView.getHeight(),
        Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    rootView.draw(c);
    return b;
}

private Bitmap getBitmapFromView2(View rootView) {
    rootView.setDrawingCacheEnabled(true);
    Bitmap bm = Bitmap.createBitmap(rootView.getDrawingCache());
    rootView.setDrawingCacheEnabled(false);

    return bm;
}

I also tried to add buildDrawingCache and destroyDrawingCache to different places of the second function.

In rootView I have a FrameLayout with a RecyclerView inside. Sometimes after scrolling this RecyclerView and asking for a snapshot I get an image with older scroll position. I have this effect for both functions.

The interesting thing is that both work fine on the other screen, where rootView is DrawerLayout and it also contains RecyclerView

How to always get a fresh snapshot?

darja
  • 4,985
  • 9
  • 33
  • 47

1 Answers1

1

Disabling hardware acceleration for RecyclerView helped. I added it to OnScrollListener to avoid lags on scrolling. Seems to work.

private RecyclerView.OnScrollListener mSwitchAccelerationListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            mRecyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        } else {
            mRecyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        }
    }
};

Useful post: Efficient "Screenshots" of a View?

Community
  • 1
  • 1
darja
  • 4,985
  • 9
  • 33
  • 47