0
Follow
0
View

Android dynamic drawing curve cannot be drawn in real time

dskfepay 注册会员
2023-02-27 17:33

invalidate() after updatetime call; Method can be redrawn, 16ms refresh the interface once, you are not very fast

cscwan 注册会员
2023-02-27 17:33

doesn't do much and I can actually draw

in real time by clicking it manually

dwwsgg 注册会员
2023-02-27 17:33

Does it trigger the draw event in onResume? You can add a button, click on it, execute the redraw method and try

duhuafeng2006 注册会员
2023-02-27 17:33

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.

darkplume 注册会员
2023-02-27 17:33

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.

dacong0417 注册会员
2023-02-27 17:33
< div class = "md_content_show e397 data - v - 3967" = "" >

thank you resolved

About the Author

Question Info

Publish Time
2023-02-27 17:33
Update Time
2023-02-27 17:33

Related Question

使用Arduino Due的Android USB附件无法重新启动

关于#Android#的问题,如何解决?

[Android]How can I return home screen with faded animation

C:\Users\Admin\AppData\Local\Android\Sdk\platform-tools\adb -s simulator -5554 shell getprop

Android Studio推送被拒绝[重复]

Android studio

android外设应用原理

缩小布局在android - Java

在尝试整合facebook登录时,在android manifest文件中获取错误android资源链接失败

Android工作室AVD管理器丢失