Animation插值器:解决Android Animation 循环执行的停顿问题
来源:互联网 发布:2016做淘宝还有前途吗 编辑:程序博客网 时间:2024/04/28 22:02
在Android开发中,有时候我们需要一个动画一直循环执行下去,常见的如laoding菊花一直旋转,这时候就需要使用Animation的repeat功能,如下:
animation = new RotateAnimation(0f, 360f, lightView.getWidth() / 2, lightView.getHeight() / 2);animation.setDuration(2500);animation.setRepeatMode(Animation.RESTART);animation.setRepeatCount(Animation.INFINITE);lightView.startAnimation(animation);
为lightView执行以中心为圆点的旋转动画。
repeatMode设为RESTART,在动画执行结束后继续从头执行。由于单次动画是旋转一周,这样两次动画可以衔接在一起。
repeatCount设为INFINITE来保证无限循环执行动画。
在实际的展示效果中,你会发现每次动画播放完都会停顿一下再去播放下次动画,动画无法连贯的执行。
其实并不是动画间有停顿,而是animation的插值器的效果。
解决方法很简单,为Animation设置线性插值器即可:
animation.setInterpolator(new LinearInterpolator());
这样我们解决了问题,但是为什么要这么做呢?
来看看Animation的构造函数源码:
public Animation() { ensureInterpolator();}public Animation(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Animation); ... ensureInterpolator();}
可以看到构造函数中都会调用ensureInterpolator函数,这个函数源码如下:
protected void ensureInterpolator() { if (mInterpolator == null) { mInterpolator = new AccelerateDecelerateInterpolator(); }}
所以,Animation默认会使用AccelerateDecelerateInterpolator这个插值器,这个插值器的作用是动画播放有个加速减速的过程,类似汽车启动和刹车的效果,开始播放时会从静止状态逐渐加速直到匀速,接近结束时会逐渐减速直至停止。所以当我们未设置其他插值器的时候,由于这个插值器的作用导致动画效果上无法连贯,有一个停顿的效果。
而线性插值器LinearInterpolator的效果是动画全程匀速执行,这样当我们设置了这个插值器动画就可以连贯起来。
插值器极大的丰富了Animation的效果,我们可以通过系统自带的或者自定义的插值器来完成很多酷炫的效果,大家有兴趣可以找资料看一下。
阅读全文
0 0
- Animation插值器:解决Android Animation 循环执行的停顿问题
- 解决animation循环中停止时卡顿一下的问题
- 解决animation循环中停止时卡顿一下的问题
- 解决Animation 添加AnimationClip 无效的问题
- css3 animation动画,设置循环间的延时执行
- Android Animation无限循环动画
- Android animation 加载延时的问题
- Android Animation 遇到的一些问题
- Android的animation
- Android 的 Tweened animation
- Android的Animation
- Android的animation(动画)
- Android的animation
- android Animation的使用
- Android的animation
- Android的 animation
- android 的Animation
- Android的animation动画
- 机器学习方差和偏差权衡(Understanding the Bias-Variance Tradeoff)
- Kotlin学习(二)—— 基本语法函数变量,字符串模板条件表达式,null,类型检测,for,while,when,区间,集合
- 前端学习之路(一)
- vue如何在页面加载时不让显示花括号{{}}
- 更巧妙的表单设计与登陆访问
- Animation插值器:解决Android Animation 循环执行的停顿问题
- 页面合并多列相同的数据
- javascript闭包
- spring mvc 整合 thymeleaf 使用java配置实现
- ELK 6.0搭建整理
- 打开Eclipse提示“The default workspace “xxxx” is in use or cannot be created Please choose a different on
- 密码生成 (利用数学原理解题,不明白,先记录)
- Effective C++ — 条款42:了解typename的双重意义
- 身份证识别、驾驶证识别、行驶证识别