Pink Jazz
Pink Jazz

Reputation: 802

Android - How to overlay one path on top of another

I currently have the following code:

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 app, I at first draw a red path, with its coordinates stored in the ArrayLists pathArrayX and pathArrayY. I am tracking the X and Y coordinates of a circular ImageView being moved underneath a mouse, and would like to overlay the red path with a green path when the user hovers over the path from beginning to end. As the user hovers over the red path, the portion of the red path that they already completed would be overlaid by a green path along the same segment. The X and Y coordinates of the ImageView (curX and curY) are being calculated from a running thread.

However, my app doesn't appear to be drawing the green path at all. Is there anything I am doing wrong here?

Upvotes: 4

Views: 333

Answers (1)

Billy
Billy

Reputation: 1374

Is this function even being called?

Assuming it's being called inside onDraw(Canvas), it looks like it might be missing the outer code for a loop. Seeing that you're doing pathIndex++ at the end, were you using a while loop? If you're just going to loop through point, use a for-loop as while-loop is more prone to dropping into an endless loop if you forgot to increment counter or doing wrongly, or do it in multiple places.

Side notes: if the boolean start flag is only being used to lazily initialise greenPath, you should scrap that and just use if (greenPath == null){ as a general practise. Use states that you can directly infer from objects and not use flags if you can help it, this makes code cleaner.

Upvotes: 3

Related Questions