invalidate() after updatetime call; Method can be redrawn, 16ms refresh the interface once, you are not very fast
Android implements dynamic drawing curve, but cannot draw in real time. Finally, exit to the mobile desktop, and the curve will be drawn when entering the app. Repeat exit from the desktop to enter the app, and the curve will be displayed. The coordinates of the code are correct, but the problem is not drawing in real time
0 Answer
invalidate() after updatetime call; Method can be redrawn, 16ms refresh the interface once, you are not very fast
doesn't do much and I can actually draw
in real time by clicking it manually
Does it trigger the draw event in onResume? You can add a button, click on it, execute the redraw method and try
I added a button and it can be drawn in real time by manually clicking it, but there is a problem with automatic drawing in enlarged code.
Using GPT and yourself, you can see from your code that after each update, you call the toDrawLine(myCanvas) method to draw the curve, However, there is no real-time curve drawing in this method, but it is drawn in onDraw(Canvas canvas) method. Therefore, every time the toDrawLine(myCanvas) method is called, a new point is just added to the point list xyList, and the curve is not immediately drawn.
When you exit the application to the desktop and then enter the application again, the onDraw(Canvas canvas) method is called and a curve is drawn for all the points, including the newly added points. Therefore, if you exit the application multiple times and re-enter, you will see an increasing curve.
To draw the curve in real time, you need to move the drawing code in the toDrawLine(myCanvas) method into the onDraw(Canvas canvas) method so that the curve is drawn immediately every time the data is updated.
Specific implementation method is as follows:
Modifies toDrawLine(Canvas canvas) method to move drawing code into onDraw(Canvas canvas) method:
private void toDrawLine(Canvas canvas) {
if (xyList == null || xyList.size() == 0 || xyList.size() < 3) {
return;
}
newPoints = new ArrayList<>();
newPoints.addAll(xyList);
baseLinePath.moveTo(newPoints.get(newPoints.size() - 2).x, newPoints.get(newPoints.size() - 2).y);
for (int i = newPoints.size() - 2; i < newPoints.size(); i++) {
if (i >= 0) {
PointF point = newPoints.get(i);
baseLinePath.lineTo(point.x, point.y);
}
}
mPaint.setColor(Color.parseColor("#FF4F7FFF"));
canvas.drawPath(baseLinePath, mPaint);
}
Modify onDraw(Canvas canvas) method, call toDrawLine(myCanvas) method:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
myCanvas = canvas;
measure();
drawBottomLine(myCanvas);
toDrawLine(myCanvas); // 在每次绘制时调用 toDrawLine 方法
}
Removes drawing code in toDrawLine(myCanvas) that has been moved to onDraw(Canvas canvas) :
// 删除以下代码
// baseLinePath.moveTo(newPoints.get(newPoints.size() - 2).x, newPoints.get(newPoints.size() - 2).y);
//
// for (int i = newPoints.size() - 2; i < newPoints.size(); i++) {
// if (i >= 0) {
// PointF point = newPoints.get(i);
// baseLinePath.lineTo(point.x, point.y);
// }
// }
//
// mPaint.setColor(Color.parseColor("#FF4F7FFF"));
// canvas.drawPath(baseLinePath, mPaint);
This way, the curve is drawn immediately every time the data is updated, rather than having to exit the application to the desktop and enter again to see the curve.
thank you resolved
这家伙很懒,什么都没留下...