View 的 onDraw 方法在 Android 2.2 上被一次又一次调用
我是 Android 开发新手,在尝试在视图上实现图形绘制时遇到了一个棘手的问题。
我想做的是在视图中绘制图表。我将一个View放入ScrollView中,并重写View的onDraw方法并在onDraw中进行绘制。最重要的是图表需要滚动功能。 我重写的View的onDraw方法包括坐标计算和图表中许多必要的元素绘制(线,点,轴,标签等)。 正如您可以想象的那样,我计算并绘制了整个图表,不仅是可见区域,而且是包括屏幕外在内的整个区域。 现在的问题是,当我滚动 ScrollView 时,onDraw 方法会被一次又一次调用,从而导致性能问题,并且滚动视图运行速度非常慢。我试图找到一种方法来防止在第一次调用后调用 onDraw 但没有运气。一旦 onDraw 被调用,我就必须一次又一次地计算同样的事情,这是没有必要的。
你们有什么答案吗?谢谢。
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 trying 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 mScrollX 和 mScrollY 来计算画布的哪个可见部分需要重新绘制。
You can use mScrollX and mScrollY to calculate which visible part of canvas need to be re-draw.