在Android上为画布路径的绘制设置动画

在Android上为画布路径的绘制设置动画,android,graphics,canvas,Android,Graphics,Canvas,我希望为路径的绘制设置动画,即使其逐渐显示在屏幕上。我正在使用画布,到目前为止,我的最佳猜测是使用ObjectAnimator来处理动画。但是,我不知道如何在onDraw()方法中实际绘制相应的路径段。有没有一种方法可以做到这一点?我需要考虑路径效应吗 编辑:使用DashPathEffect并在动画中设置其“开”和“关”间隔,以覆盖我们要为该步骤绘制的路径部分,在这里似乎可行,但需要为动画的每个步骤分配新的DashPathEffect。如果有更好的方法,我会把这个问题留待讨论。据我所知,唯一的方

我希望为路径的绘制设置动画,即使其逐渐显示在屏幕上。我正在使用画布,到目前为止,我的最佳猜测是使用ObjectAnimator来处理动画。但是,我不知道如何在onDraw()方法中实际绘制相应的路径段。有没有一种方法可以做到这一点?我需要考虑路径效应吗


编辑:使用DashPathEffect并在动画中设置其“开”和“关”间隔,以覆盖我们要为该步骤绘制的路径部分,在这里似乎可行,但需要为动画的每个步骤分配新的DashPathEffect。如果有更好的方法,我会把这个问题留待讨论。

据我所知,唯一的方法是从一个空路径开始,并有一个runnable,它以定义的间隔向路径添加点,直到它完成。

回答我自己的问题,因为我找到了一个令人满意的方法

诀窍是使用
ObjectAnimator
逐步更改笔划的当前长度,使用
DashPathEffect
控制当前笔划的长度。
DashPathEffect
的破折号参数最初设置为:

float[] dashes = { 0.0f, Float.MAX_VALUE };
第一个浮动是可见笔划的长度,第二个浮动是不可见部分的长度。第二个长度被选为非常高。因此,初始设置对应于完全不可见的笔划

然后,每当对象动画师更新笔划长度值时,将使用新的可见部分创建一个新的
DashPathEffect
,并将其设置为画家对象,视图将无效:

dashes[0] = newValue;
mPaint.setPathEffect(new DashPathEffect(dashes, 0));
invalidate();
最后,onDraw()方法使用该画师绘制路径,该路径将只包含我们想要的部分:

canvas.drawPath(path, mPaint);

我看到的唯一缺点是,我们必须在每个动画步骤中创建一个新的DashPathEffect(因为它们不能重复使用),但从整体上看,这是令人满意的-动画既漂亮又平滑。

这将生成一个非常粗糙的动画。假设路径只包含一条长曲线-它只有一个起点和终点以及两个控制点。使用此方法无法平滑地设置动画。然后,使用PathEffect可以获得更好的效果。您仍然需要runnable来更改重绘之间的PathEffect。是的,这由ObjectAnimator负责。DashPathEffect似乎是目前为止最好的方法,但它需要在每个步骤分配一个新的效果实例-我想知道我们是否可以避免这种情况。但我也想画一条虚线!为什么我们应该将相位值保持为0。你能解释一下价值阶段背后的逻辑吗?
package com.nexoslav.dashlineanimatedcanvasdemo;

import android.animation.ValueAnimator;
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.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

public class CustomView extends View {
    float[] dashes = {30, 20};
    Paint mPaint;
    private Path mPath;

    private void init() {

        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(10f);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setPathEffect(new DashPathEffect(dashes, 0));

        mPath = new Path();
        mPath.moveTo(200, 200);
        mPath.lineTo(300, 100);
        mPath.lineTo(400, 400);
        mPath.lineTo(1000, 200);
        mPath.lineTo(1000, 1000);
        mPath.lineTo(200, 400);

        ValueAnimator animation = ValueAnimator.ofInt(0, 100);
        animation.setInterpolator(new LinearInterpolator());
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                Log.d("bla", "bla: " + valueAnimator.getAnimatedValue());
                mPaint.setPathEffect(new DashPathEffect(dashes, (Integer) valueAnimator.getAnimatedValue()));
                invalidate();
            }
        });
        animation.setDuration(1000);
        animation.setRepeatMode(ValueAnimator.RESTART);
        animation.setRepeatCount(ValueAnimator.INFINITE);
        animation.start();
    }

    public CustomView(Context context) {
        super(context);
        init();
    }


    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawPath(mPath, mPaint);
    }
}