androidandroid-canvasandroid-graphics

How to reverse a Path object?


I have created an arch from a path object like this:

path.addArc(rect, 0f, -180f) //and this draws my path CCW. 

//and if i wanted it drawn CW:
path.addArc(rect, -180f, 180f)//reverse

but i wish i did not have to do this. i wish there was a function like: path.reverse().

I noice there is a path directions class: but i dont see how to use it with Arc.... any idea how i can reverse a path better ?


Solution

  • Instead of revert the arc, you could reverse the step counter of your animation...

    pathMeasure = new PathMeasure(yourArcPath, false);

      Matrix mxTransform = new Matrix();
        float eachStepLen = pathMeasure.getLength() / 200; //200 animation steps
        if (currentStep <= 110 && !reverseActive) {
    
            currentStep ++; // --- Going forward...
    
            pathMeasure.getMatrix(
                    eachStepLen * currentStep,
                    mxTransform,
                    PathMeasure.POSITION_MATRIX_FLAG);
    
            canvas.drawBitmap(image, mxTransform, null);
    
    
        } else {
            reverseActive = true;
    
            pathMeasure.getMatrix(
                    eachStepLen * currentStep,
                    mxTransform,
                    PathMeasure.POSITION_MATRIX_FLAG);
    
            canvas.drawBitmap(image, mxTransform, null);
    
            currentStep --; // --- Going backwards...
    
            if (currentStep == 0){
                reverseActive = !reverseActive;
            }
    
        }