Android三种属性动画的使用
16lz
2021-01-25
Android 3.0提供了属性动画,几乎可以代替补间动画。属性动画用法更加简洁,功能更多强大。使用属性动画的两个类是ValueAnimator和ObjectAnimator。
ValueAnaimator使用示例
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);//设置变化范围0到100 valueAnimator.setDuration(3000);//设置播放时间毫秒 valueAnimator.setStartDelay(1000);//设置延时时间毫秒 valueAnimator.setRepeatCount(0);//设置重复次数 valueAnimator.setRepeatMode(ValueAnimator.RESTART);//设置重复模式 valueAnimator.start();//开始播放 valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int value = (int) animation.getAnimatedValue(); //获取实时数值并更新到需要的View上 } });
ObjectAnimator比ValueAnimator更高级,不用监听数据变化,它可以直接修改指定的属性。前提是对象必须有对应的setXXX()方法和getXXX()方法。如果没有这两个方法,我们可以通过继承或者包装(装饰模式)来添加这两个方法。
ObjectAnimator使用示例
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f); objectAnimator.setDuration(300); ... //设置若干属性省略 objectAnimator.start();
以上是单一动画,如果要使用组合动画,可以使用PropertyValuesHolder。
ValueAnimator和ObjectAnimator组合动画的用法一样,下面是ObjectAnimator的组合动画使用示例
组合动画使用示例
PropertyValuesHolder translateHolder = PropertyValuesHolder.ofFloat("translationY", 0, 500); PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofFloat("alpha", 0f, 1f, 0f); ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, alphaHolder, translateHolder); objectAnimator.setDuration(3600); objectAnimator.setStartDelay(1000); objectAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); //监听动画结束 } }); objectAnimator.start();
如果要修改的属性不是数值类型的,我们可以使用对应的ofObject方法:
ValueAnimator.ofObject方法源码:
public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) { ValueAnimator anim = new ValueAnimator(); anim.setObjectValues(values); anim.setEvaluator(evaluator); return anim; }
ObjectAnimator.ofObject方法源码:
public static ObjectAnimator ofObject(Object target, String propertyName, TypeEvaluator evaluator, Object... values) { ObjectAnimator anim = new ObjectAnimator(target, propertyName); anim.setObjectValues(values); anim.setEvaluator(evaluator); return anim; }
ofObject(…)方法需要传入一个估值器TypeEvaluator。
关于估值器
下面是自带的IntEvaluator源代码。我们只需要重写evaluate这个方法即可。
其中fraction是百分比,是系统自动计算出来的,而动画进度值是需要我们自己去算的。计算规则根据情况而定。
/** * This evaluator can be used to perform type interpolation between int
values. */public class IntEvaluator implements TypeEvaluator<Integer> { /** * This function returns the result of linearly interpolating the start and end values, with * fraction
representing the proportion between the start and end values. The * calculation is a simple parametric calculation: result = x0 + t * (v1 - v0)
, * where x0
is startValue
, x1
is endValue
, * and t
is fraction
. * * @param fraction The fraction from the starting to the ending values * @param startValue The start value; should be of type int
or * Integer
* @param endValue The end value; should be of type int
or Integer
* @return A linear interpolation between the start and end values, given the * fraction
parameter. */ public Integer evaluate(float fraction, Integer startValue, Integer endValue) { int startInt = startValue; return (int)(startInt + fraction * (endValue - startInt)); }}
更多相关文章
- Animation(2、帧动画)
- ProgressBar自定义样式中的层叠图片显示问题
- Android(安卓)ListView 分页功能,包含前端分页,服务器分页
- 自定义ViewGroup获取子View参数
- 灵活使用Android中ActionBar和ViewPager切换页面
- Android(安卓)程序中调用其他程序的方法总结
- 2.2.1 LinearLayout(线性布局)
- Android(安卓)在Java代码中设置style属性--使用代码创建Progress
- startService与bindService