【问题标题】:onDraw() is not called in custom View自定义视图中未调用 onDraw()
【发布时间】:2013-07-04 12:42:57
【问题描述】:

对于我正在开发的应用程序,我想将计步器的结果绘制成图表。计步器使用service 来测量某人在后台的步数。我有一个名为 DrawView 的自定义视图,我想在其中绘制我的结果。每当测量步数时,我都会从service 调用 drawPoint 方法。然后从我的 drawPoint 方法中,我尝试使用invalidate() 调用onDraw()。根据我的日志,正在调用 drawPoint,但没有调用 onDraw()。所以我的问题是:

为什么 onDraw() 没有被调用,我应该如何让 DrawView 调用它?


public class DrawView extends View {

...

@Override
public void onDraw(Canvas canvas) {
    for (Point point : points) {
        canvas.drawCircle(point.x, point.y, 5, paint);
        Log.d("Checks", "onDraw is called");
    }
}

public void drawPoint(int counter) {
    Log.d("Checks", "Point coordinates: " + 40 + ", " + counter);

    Point point = new Point();
    point.y = 40;
    point.x = counter;
    points.add(point);
    invalidate();
}
}

【问题讨论】:

  • 你在哪里调用 drawPoint() ?
  • 而不是直接从服务调用 view.drawPoint(),而是使用处理程序来更新 ui。

标签: android view ondraw


【解决方案1】:

而不是直接从服务调用 view.drawPoint(),而是使用处理程序来更新 ui。

查看链接update ui from background service

【讨论】:

    【解决方案2】:

    invalidate() 必须从 UI 线程调用。尝试调用 postInvalidate()。

    http://developer.android.com/reference/android/view/View.html#invalidate()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2016-09-10
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      相关资源
      最近更新 更多