View 的 onDraw 方法在 Android 2.2 上被一次又一次调用

发布于 2024-10-09 16:43:41 字数 401 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我也只是我 2024-10-16 16:43:41

您可以使用 mScrollX 和 mScrollY 来计算画布的哪个可见部分需要重新绘制。

 /**
 * 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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文