Animating the drawing of a canvas path on Android

前端 未结 3 590
北荒
北荒 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:12

    Answering my own question, as I figured out a satisfying way to do that.

    The trick is to use an ObjectAnimator to progressively change the current length of the stroke, and a DashPathEffect to control the length of the current stroke. The DashPathEffect will have its dashes parameter initially set to the following:

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

    First float is the length of the visible stroke, second length of non-visible part. Second length is chosen to be extremely high. Initial settings thus correspond to a totally invisible stroke.

    Then everytime the object animator updates the stroke length value, a new DashPathEffect is created with the new visible part and set to the Painter object, and the view is invalidated:

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

    Finally, the onDraw() method uses this painter to draw the path, which will only comprise the portion we want:

    canvas.drawPath(path, mPaint);
    

    The only drawback I see is that we must create a new DashPathEffect at every animation step (as they cannot be reused), but globally this is satisfying - the animation is nice and smooth.

提交回复
热议问题