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?