Animating the drawing of a canvas path on Android

前端 未结 3 587
北荒
北荒 2020-12-16 04:14

I\'d like to animate the drawing of a path, i.e. to have it progressively appear on the screen. I am using the canvas and my best guess so far is to use an ObjectAnimator to

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 05:00

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

提交回复
热议问题