I am new on Android development and meet with a tough problem when trying to implement a graphic drawing on a View.
What I want to do is drawing a chart in a View. I put a View in a ScrollView and override the onDraw method of View and do drawing in onDraw. The most important thing is that the chart needs scroll functions. The View's onDraw method I override includes the coordinate calculation and many necessary elements drawing (line, dot, axis, label, etc) in a chart. As you can imagine, I calculate and draw the whole chart not only the visible area but the whole area including offscreen. Now the problem is that onDraw method get called again and again when I scroll the ScrollView, so that it causes a performance issue and the scroll view running very slow. I was tryi开发者_如何学Cng to find a way to prevent the calling of onDraw after first call but no lucky. I have to calculate the same thing again and again once onDraw get called which is not necessary.
Do you guys have any answer? Thanks.
You can use mScrollX and mScrollY to calculate which visible part of canvas need to be re-draw.
/**
* The offset, in pixels, by which the content of this view is scrolled
* vertically.
* {@hide}
*/
@ViewDebug.ExportedProperty
protected int mScrollY;
/**
* The left padding in pixels, that is the distance in pixels between the
* left edge of this view and the left edge of its content.
* {@hide}
*/
@ViewDebug.ExportedProperty
protected int mPaddingLeft;
/**
* The right padding in pixels, that is the distance in pixels between the
* right edge of this view and the right edge of its content.
* {@hide}
*/
@ViewDebug.ExportedProperty
protected int mPaddingRight;
/**
* The top padding in pixels, that is the distance in pixels between the
* top edge of this view and the top edge of its content.
* {@hide}
*/
@ViewDebug.ExportedProperty
protected int mPaddingTop;
/**
* The bottom padding in pixels, that is the distance in pixels between the
* bottom edge of this view and the bottom edge of its content.
* {@hide}
*/
@ViewDebug.ExportedProperty
protected int mPaddingBottom;
精彩评论