After extending the ScrollView
class I was able to easily be notified of the scrolling in realtime.
Now I need to capture the content of this scrollview in a very specific part. Let's say I want to capture the top of the screen (matching parent width and a defined height, like 100dp). But only the content of the ScrollView and not the rest, if there is anything else on the top but not as part of the ScrollView.
I tried using on the scrollview :
setDrawingCacheEnabled(true);
getDrawingCache(true);
setDrawingCacheEnabled(false);
Then I tried to crop so that I get the part I want :
Bitmap.createBitmap(complete, 0, 0, width, height);
Results are very far from what I want to achieve and performance are very very poor and at some point I would get either a SIGENV
or getDrawingCache(true)
tries to use a recycled bitmap...
So how can I easily capture the content in the desired area without too much performance hit ?
Note: this process must be done as I am scrolling the content, so inside ScrollView's onScrollChanged(final int x, final int y)
.
Thanks !
Since the problem was fun I implemented it, it seems to work fine. I guess that you are recreating a Bitmap each time that's why goes slow.
The idea is like this, you create an area in the ScrollView that you want to copy (see
Rect cropRect
andBitmap screenshotBitmap
), it's full width and you just need to set the height. The view automatically set a scroll listener on itself and on every scroll it will copy that area. Note thatsetDrawingCacheEanbled(true)
is called just once when the view is instantiated, it basically tells the view that you will callgetDrawingCache()
, which will return the Bitmap on which the view is drawing itself. It then copy the area of interest on screenshotBitmap and that's the Bitmap that you might want to use.ScreenshottableScrollView.java
activity_main.xml
MainActivity.java