Android – how to overlay one path on another

I currently have the following codes:

private void drawGreen(Canvas canvas) {


    greenPaint.setColor(0xFF00AA00);

    if (start) {
        greenPath = new Path();
        greenPath.reset();
        greenPath.moveTo(pathArrayX.get(0), pathArrayY.get(0));
        start = false;
    }

    if (isInsideCircle(pathArrayX.get(pathIndex), pathArrayY.get(pathIndex), curX, curY, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, getResources().getDisplayMetrics()))) {
        greenPath.lineTo(pathArrayX.get(pathIndex), pathArrayY.get(pathIndex));
        canvas.drawPath(greenPath, greenPaint);
        pathIndex++;

    }


}

private boolean isInsideCircle(float x, float y, float centerX, float centerY, float radius) {
    return Math.pow(x - centerX, 2) + Math.pow(y - centerY, 2) < Math.pow(radius, 2);
}

In my application, I first draw a red path whose coordinates are stored in ArrayLists patharrayx and patharrayy. I am tracking the X and Y coordinates of the circular ImageView moving under the mouse, and when the user hovers over the path from beginning to end, I want to cover the red path with a green path. When the user hovers over the red path, The part of the red path they have completed will be covered by the green path along the same segment. The X and Y coordinates of ImageView (curx and cury) are calculated from the running thread

However, my application doesn't seem to draw a green path at all. Am I wrong here?

resolvent:

Is this function even called?

Suppose it is called in OnDraw (canvas), it seems that it may lack the external code of the loop. See that you are doing pathindex at last, are you using the while loop? If you just want to loop through points, use the for loop, because if you forget to increase the counter or make an error, or execute in multiple places, the while loop is more likely to fall into an infinite loop

Note: if the Boolean start flag is only used to lazily initialize greenpath, it should be discarded and if (greenpath = = null) {should be used as a general practice. Use the state that can be inferred directly from the object. If you can help it, do not use the flag, which makes the code clearer

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>