Custom onDraw view inside scroll

I have a custom view (width = 2000) inside a horizontal scroll (width = 480). So there is a scrollable area.

When onDraw() is called, the dirty rectangle ( getClipBounds() returns) returns all the dimensions of the view, so I draw the whole view, including the area that is not visible. As a result, when I scroll, onDraw() no longer called because the areas that become visible are already drawn and are somehow remembered.

 public void onDraw(Canvas canvas) { canvas.getClipBounds(r); // returns 2000 x 400 } 

This works great !!!
However, my user view can be as wide as 20,000 or more, and everything starts to slow down. My concern is that the cached drawing uses a lot of memory. I don’t think that the drawing is saved as a bitmap, because it has already crashed, since these drawing commands (lines and text, basically) are saved? Is there a way to indicate that onDraw() should request only the visible part of the view, and when scrolling, it calls onDraw() ? Or is there a different approach?

Thanks!

+7
android view android canvas ondraw
source share
1 answer

Android uses hardware accelerated display lists to draw with Android 3.0. This caches the display list for the entire view.

If the view in ScrollView is really large, you can turn off hardware acceleration for the view.

We do this by subclassing ScrollView and overriding the onAttachedToWindow() method as follows:

 @Override protected void onAttachedToWindow() { setLayerType(LAYER_TYPE_SOFTWARE, null); } 
+3
source share

All Articles