动画在Android画布路径的绘制 [英] Animating the drawing of a canvas path on Android

查看:256
本文介绍了动画在Android画布路径的绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动画路径的绘制,即把它逐渐出现在屏幕上。我现在用的是帆布和我最好的猜测,到目前为止是使用ObjectAnimator利用动画的照顾。但是,我想不出如何实际绘制中的OnDraw()方法路径的对应段落。有没有一种方法,允许这样做?我会需要涉及到该路径的影响?

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 take care of the animation. However, I cannot figure out how to actually draw the corresponding segment of the path in the onDraw() method. Is there a method that would allow to do this? Would I need to involve path effects for that?

编辑:使用DashPathEffect和开和关的时间间隔设置其动画,以支付我们要画的那一步,似乎在这里工作路径的一部分,但它要求对动画的每一步都分配一个新DashPathEffect。我将离开这个问题打开的情况下有更好的方法。

Using a DashPathEffect and setting its "on" and "off" intervals in the animation to cover the part of the path we want to draw for that step seems to work here, but it requires allocating a new DashPathEffect for every step of the animation. I will leave the question open in case there is a better way.

推荐答案

回答我的问题,我想出了一个令人满意的方式来做到这一点。

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

诀窍是使用 ObjectAnimator 来逐步改变行程的当前长度,和 DashPathEffect 来控制当前行程的长度。该 DashPathEffect 将初步设置为以下的破折号参数:

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.

然后每次对象动画更新行程长度值,一个新的 DashPathEffect 与新的可见部分创建并设置为画家对象,并认为是无效的:

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

最后,的OnDraw()方法使用该画家绘制的路径,这将只包括我们想要的部分:

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

canvas.drawPath(path, mPaint);

我看到的唯一缺点是,我们必须在每一个动画步创建一个新的DashPathEffect(因为他们不能重复使用),但全球范围内,这是令人满意的 - 动画是好的,平稳

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.

这篇关于动画在Android画布路径的绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆