如何在画布上以恒定的速度在Android上慢慢绘制几条线?

时间:2018-06-04 06:53:26

标签: android-animation android-canvas

我需要抓取标记在Android上的画布上绘制一个图形,效果就像跟随gif一样:

enter image description here

嗯,到目前为止,我可以通过 ValueAnimator 绘制一个具有恒定速度的边。但是,我只能一次绘制一面,因为在绘制下一面时我无法保存最后一面。那么,有没有一种解决问题的好方法呢?

ValueAnimator缓慢划线的代码:

GraphicsView.java

public class GraphicsView extends View {
    private int stepX, stepY = 0;
    private int startX, startY, stopX, stopY = 0;
    private Paint paint = null;

    public GraphicsView(Context context) {
        super(context);

        // Paint
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);

        startX = 40;
        startY = 397;
        stopX  = 1040; 
        stopY  = 397;

        Init();
    }

    public void Init(){
        ValueAnimator animatorX = ValueAnimator.ofFloat(startX, stopX);
        ValueAnimator animatorY = ValueAnimator.ofFloat(startY, stopY);

        animatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                stepX = Math.round((Float)valueAnimator.getAnimatedValue()); invalidate();
            }
        });
        animatorY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                stepY = Math.round((Float)valueAnimator.getAnimatedValue()); invalidate();
            }
        });

        AnimatorSet set = new AnimatorSet();
        LinearInterpolator l = new LinearInterpolator();
        set.setInterpolator(l);
        set.setDuration(3000);
        set.playTogether(animatorX, animatorY);
        set.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(startX, startY, stepX, stepY, paint);
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {   
    private Display display         = null;  
    private GraphicsView view       = null;  
    private ConstraintLayout layout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        display = getWindowManager().getDefaultDisplay();
        layout = (ConstraintLayout)findViewById(R.id.main_layout);

        view = new GraphicsView(this);
        view.setMinimumWidth(display.getWidth());
        view.setMinimumHeight(display.getHeight());   
        layout.addView(view);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用ObjectAnimator类进行回调 每当您想绘制更多路径时,都可以使用其中一种方法。

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;
import android.util.Log;

public class PathView extends View
{
    Path path;
    Paint paint;
    float length;

    public PathView(Context context)
    {
        super(context);
    }

    public PathView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public PathView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }

    public void init()
    {
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.STROKE);

        path = new Path();
        path.moveTo(50, 50);
        path.lineTo(50, 500);
        path.lineTo(200, 500);
        path.lineTo(200, 300);
        path.lineTo(350, 300);

        // Measure the path
        PathMeasure measure = new PathMeasure(path, false);
        length = measure.getLength();

        float[] intervals = new float[]{length, length};

        ObjectAnimator animator = ObjectAnimator.ofFloat(PathView.this, "phase", 1.0f, 0.0f);
        animator.setDuration(3000);
        animator.start();
    }

    //is called by animtor object
    public void setPhase(float phase)
    {
        Log.d("pathview","setPhase called with:" + String.valueOf(phase));
        paint.setPathEffect(createPathEffect(length, phase, 0.0f));
        invalidate();//will calll onDraw
    }

    private static PathEffect createPathEffect(float pathLength, float phase, float offset)
    {
        return new DashPathEffect(new float[] { pathLength, pathLength },
            Math.max(phase * pathLength, offset));
    }

    @Override
    public void onDraw(Canvas c)
    {
        super.onDraw(c);
        c.drawPath(path, paint);
    }
}

然后,只需像这样调用init()开始动画(或者,如果您希望它在视图膨胀后立即开始,则将init()调用放入构造函数中):

PathView path_view = (PathView) root_view.findViewById(R.id.path);
path_view.init();

也请参见此问题here

Using Value Animator Example

Reference 1

Reference 2

Reference 3