1. 程式人生 > 其它 >2020-12-11繪製跟隨路徑移動的圓圈

2020-12-11繪製跟隨路徑移動的圓圈

技術標籤:AndroidJAVA高階

繪製跟隨路徑移動的圓圈

public void onDraw(Canvas canvas){

        Path sPath = new Path();
        sPath.moveTo(100, 100);
        sPath.lineTo(300, 100);
        sPath.lineTo(300, 300);
        sPath.lineTo(100,300);
        sPath.lineTo(100,100);
        sPath.close();

        Paint ballPaint = new Paint();
        ballPaint.setColor(Color.GREEN);
        Paint pathPaint = new Paint();
        pathPaint.setColor(Color.BLUE);

        canvas.drawPath(sPath, ballPaint);
        canvas.drawCircle(100,100,20,pathPaint);
    }

int iCurStep = 0;// current animation step

@Override
protected void onDraw(Canvas canvas) {
    PathMeasure pm = new PathMeasure(sPath, false);
    float fSegmentLen = pm.getLength() / 20;//we'll get 20 points from path to animate the circle
    float afP[] = {0f, 0f};

    if (iCurStep <= 20) {
        pm.getPosTan(fSegmentLen * iCurStep, afP, null);
        canvas.drawCircle(afP[0],afP[1],20,pathPaint);
        iCurStep++;
       // invalidate();
    } else {
        iCurStep = 0;
    };
};