自定义视图中未调用 onDraw() [英] onDraw() is not called in custom View
问题描述
对于我正在开发的应用程序,我想绘制计步器的结果.计步器使用 service
来测量某人在后台执行的步数.我有一个名为 DrawView 的自定义视图,我想在其中绘制结果.每当测量一个步骤时,我都会从我的 service
调用 drawPoint 方法.然后从我的 drawPoint 方法中,我尝试使用 invalidate()
调用 onDraw()
.根据我的日志, drawPoint 被调用但 onDraw()
不是.所以我的问题是:
For an app I am developing, I want to graph the results from a pedometer. The pedometer uses a service
to measure the amount of steps someone makes in the background. I have a custom view called DrawView, in which I want to draw my results. I call the drawPoint method from my service
whenever a step is measured. Then from my drawPoint method I try to call onDraw()
using invalidate()
. According to my logs, drawPoint is being called but onDraw()
is not. So my question is:
为什么没有调用 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();
}
}
推荐答案
不是直接从 service 调用 view.drawPoint(),而是使用 handler 来更新 ui.
Instead calling directly view.drawPoint() from service, use handler to update the ui.
检查链接 从后台服务更新用户界面
这篇关于自定义视图中未调用 onDraw()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!