Android move object along a path

后端 未结 5 1689
天涯浪人
天涯浪人 2020-12-14 18:33

I ave created a path a circle and displayed both of them on screen as follows:

public void onDraw(Canvas canvas){

        Path sPath = new Path();
        s         


        
5条回答
  •  太阳男子
    2020-12-14 19:25

    Yes, it's possible to move image along path. I will provide simple solution to show the principle. The following code will animate the circle along the path.

    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;
        };
    };
    

提交回复
热议问题