【问题标题】:Animating the drawing of a canvas path on Android在 Android 上为画布路径绘制动画
【发布时间】:2020-02-25 05:29:29
【问题描述】:

我想对路径的绘制进行动画处理,即让它逐渐出现在屏幕上。我正在使用画布,到目前为止我最好的猜测是使用 ObjectAnimator 来处理动画。但是,我无法弄清楚如何在 onDraw() 方法中实际绘制路径的相应段。有没有一种方法可以做到这一点?我需要为此加入路径效果吗?

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

【问题讨论】:

    标签: android graphics canvas


    【解决方案1】:

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

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

    float[] dashes = { 0.0f, Float.MAX_VALUE };
    

    第一个浮动是可见笔划的长度,第二个是不可见部分的长度。第二长度选择得非常高。因此,初始设置对应于完全不可见的笔画。

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

    dashes[0] = newValue;
    mPaint.setPathEffect(new DashPathEffect(dashes, 0));
    invalidate();
    

    最后,onDraw() 方法使用这个painter来绘制路径,它只会包含我们想要的部分:

    canvas.drawPath(path, mPaint);
    

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

    【讨论】:

    • 但我也想画虚线!
    • 为什么我们要把相位值保持为0。你能解释一下值相位背后的逻辑吗?
    【解决方案2】:
    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);
        }
    }
    

    【讨论】:

      【解决方案3】:

      据我所知,唯一的方法是从一个空路径开始,并有一个可运行对象,以定义的时间间隔将点附加到路径,直到完成。

      【讨论】:

      • 那将是一个非常粗糙的动画。假设路径仅由一条长曲线组成 - 它只有一个起点和终点以及两个控制点。使用这种方法无法平滑地制作动画。
      • 然后,使用 PathEffect 会得到更好的结果。您仍然需要 runnable 在重绘之间更改 PathEffect。
      • 是的,这由 ObjectAnimator 处理。 DashPathEffect 似乎是迄今为止最好的方法,但它需要在每一步分配一个新的效果实例——我想知道我们是否可以避免这种情况。
      猜你喜欢
      • 2021-06-25
      • 2013-05-18
      • 2019-04-12
      • 2012-08-15
      • 2020-11-02
      • 1970-01-01
      • 2013-12-20
      相关资源
      最近更新 更多