如何使画布路径闪烁?

时间:2020-05-18 09:29:38

标签: android android-animation android-canvas

我使用DashPathEffect在画布上绘制路径,并在此路径上显示闪烁效果。对于路径上的闪烁效果,从我的活动onCreate()开始,我调用了以下代码来闪烁我的自定义视图。

Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50); 
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
myJourney.startAnimation(anim);  

这是我要在其上显示路径闪烁的视图类:

public class MyJourney extends View {

    Paint paint;
    Path path;

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

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

    public MyJourney(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
    }

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

        path = new Path();
        paint.setColor(Color.GRAY);
        paint.setStrokeWidth(8);

        path.moveTo(24, 23);
        path.cubicTo(33, 347, 327, 168, 432, 489);

        DashPathEffect dashPath = new DashPathEffect(new float[]{15, 15}, (float) 5.0);
        paint.setPathEffect(dashPath);

        canvas.drawPath(path, paint);
    }
}

如何使DashPath看起来像眨眼?

0 个答案:

没有答案