Scroll optimization

I have a scrollview that contains a custom view. The custom view is larger than the screen area and draws correctly.

However, scrolling tends to call onDraw () without stopping while scrolling, and I cannot make it smooth.

I used ScrollView.getDrawingRect () to calculate the visible part of the screen and only draw before, but it still returns the entire viewport (therefore it is optimized so as not to draw shielded areas), and not the delta between the last position and the current one. Ideally, I would like to draw only a delta, not the entire visible window.

If someone can tell me more information on how to use drawing caching, and if that helps to optimize scrolling, I would like to implement it or any other possible solutions that would be very grateful.

+7
source share
3 answers

When scrolling through the contents, the entire viewport needs to be redrawn because all the content has been moved. I don’t think that something needs to be done to optimize the ScrollView - if the scrolling is slow, then the method of drawing your custom view is too slow.

Try to avoid creating an object in your drawing methods, which is usually the main culprit in poor drawing performance.

Edit: Also, scrollview can quickly split old content up or down, which is still displayed on the screen, and then request to redraw only the β€œnew” part of the screen. (applies only to opaque views).

+5
source

I ran into the same problem. I solved this using the setDrawingCacheEnabled(true) function. By enabling this option, your canvas view will be cached as a bitmap, so you do not need to call the canvas drawing method every time onDraw() called.

In your custom view designer, you'll need something like this:

 public CustomView(Context context) { setDrawingCacheEnabled(true); drawnFlag = false; } 

In your onDraw method onDraw you'll need something like this:

 public void onDraw(Canvas canvas) { if (! drawnFlag) { canvas.drawPath(...); canvas.drawPath(...); drawnFlag = true; } } 

Scrolling on this user view should now be smooth, since we only call drawing methods once.

+1
source

Afaik ScrollView sets the correct click on the canvas, which your eye falls into onDraw, so you just need to draw what's inside this rectangle. You can also implement cache bitmaps based on clip size.

-one
source

All Articles