属性动画 如何修改当前fraction
时间: 2024-05-13 22:13:36 浏览: 16
要修改当前fraction,可以通过ValueAnimator类的addUpdateListener()方法来实现。在此方法中,可以获取当前动画的fraction值,并根据需要对其进行修改。
例如,以下代码演示了如何实现一个在动画进行到一半时,将动画的速度减慢一半的效果:
```
ValueAnimator animator = ValueAnimator.ofInt(0, 100);
animator.setDuration(1000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float fraction = animation.getAnimatedFraction();
if (fraction > 0.5f) {
animation.setDuration(2000);
animation.setFloatValues(fraction, 1.0f);
}
}
});
animator.start();
```
在这个例子中,我们创建了一个ValueAnimator对象,并将其设置为从0到100的整数值,并在1秒钟内运行。然后,我们添加了一个AnimatorUpdateListener,该监听器在动画更新时被调用。在此监听器中,我们获取动画的fraction值,并检查它是否大于0.5f。如果是,我们将动画的持续时间延长到2秒,并将其值从当前fraction值变为1.0f。这将导致动画速度减慢一半,并在接下来的1秒钟内完成。最后,我们调用animator.start()方法来启动动画。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)