为了账号安全,请及时绑定邮箱和手机立即绑定

浅谈Property动画

标签:
Android

我们熟悉的Frame动画和Tween动画是Android 3.0之前的,这边就不讲解了。今天我要讲的是从3.0开始引入的Property动画。

Property动画抓住三个核心概念
1.ValueAnimator
2.ObjectAnimator
3.ViewPropertyAnimator

然后在ValueAnimator中抓住一下两个概念
1.TypeEvaluator
2.TimeInterpolator

可以说,搞清楚以上5点,一切动画尽在掌握之中

ValueAnimator

ValueAnimator是整个属性动画机制当中最核心的一个类,属性动画的运行机制是通过不断地对值进行操作来实现的,而初始值和结束值之间的动画过渡就是由ValueAnimator这个类来负责计算的。

ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);  
anim.setDuration(300);  
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  
@Override  
public void onAnimationUpdate(ValueAnimator animation) {  
    float currentValue = (float) animation.getAnimatedValue();  
    Log.d("TAG", "cuurent value is " + currentValue);  
  }  
});  
anim.start();

ObjectAnimator

相比于ValueAnimator,ObjectAnimator可能才是我们最常接触到的类,因为ValueAnimator只不过是对值进行了一个平滑的动画过渡,但我们实际使用到这种功能的场景好像并不多。而ObjectAnimator则就不同了,它是可以直接对任意对象的任意属性进行动画操作的,比如说View的alpha、rotation、translationX和scaleY这几个值,分别可以完成淡入淡出、旋转、水平移动、垂直缩放这几种动画。

ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);  
animator.setDuration(5000);  
animator.start();

AnimatorSet

AnimatorSet 提供了一个把多个动画组合成一个组合的机制,并可设置组中动画的时序关系,如同时播放,顺序播放等。

  • after(Animator anim) 将现有动画插入到传入的动画之后执行

  • after(long delay) 将现有动画延迟指定毫秒后执行

  • before(Animator anim) 将现有动画插入到传入的动画之前执行

  • with(Animator anim) 将现有动画和传入的动画同时执行

ObjectAnimator moveIn = ObjectAnimator.ofFloat(textview, "translationX", -500f, 0f);  
ObjectAnimator rotate = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f);  
ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);  
AnimatorSet animSet = new AnimatorSet();  
animSet.play(rotate).with(fadeInOut).after(moveIn);  
animSet.setDuration(5000);  
animSet.start();

TypeEvalutors

evaluate()方法当中传入了三个参数,第一个参数fraction非常重要,这个参数用于表示动画的完成度的,我们应该根据它来计算当前动画的值应该是多少,第二第三个参数分别表示动画的初始值和结束值。那么下面代码的逻辑就比较清晰了,用结束值减去初始值,算出它们之间的差值,然后乘以fraction这个系数,再加上初始值,那么就得到当前动画的值了。

public class FloatEvaluator implements TypeEvaluator { 
  public Object evaluate(float fraction, Object startValue, Object endValue) { 
    float startFloat = ((Number) startValue).floatValue(); 
    return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);}
}

TimeInterpolator

Time interplator定义了属性值变化的方式,如线性均匀改变,开始慢然后逐渐快等。在Property Animation中是TimeInterplator,在View Animation中是Interplator,这两个是一样的,在3.0之前只有Interplator,3.0之后实现代码转移至了TimeInterplator。Interplator继承自TimeInterplator,内部没有任何其他代码。

  • AccelerateInterpolator      加速,开始时慢中间加速

  • DecelerateInterpolator      减速,开始时快然后减速

  • AccelerateDecelerateInterolator 先加速后减速,开始结束时慢,中间加速

  • AnticipateInterpolator      反向 ,先向相反方向改变一段再加速播放

  • AnticipateOvershootInterpolator  反向加回弹,先向相反方向改变,再加速播放

  • BounceInterpolator        跳跃,快到目的值时值会跳跃

  • CycleIinterpolator        循环,动画循环一定次数,值的改变为一正弦函数

  • LinearInterpolator        线性,线性均匀改变

  • OvershottInterpolator       回弹,最后超出目的值然后缓慢改变到目的值

private void startAnimation() {  
Point startPoint = new Point(getWidth() / 2, RADIUS);  
Point endPoint = new Point(getWidth() / 2, getHeight() - RADIUS);  
ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);  
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  
    @Override  
    public void onAnimationUpdate(ValueAnimator animation) {  
        currentPoint = (Point) animation.getAnimatedValue();  
        invalidate();  
    }  
});  
anim.setInterpolator(new BounceInterpolator());  
anim.setDuration(3000);  
anim.start();  
}

ViewPropertyAnimator

ViewPropertyAnimator其实算不上什么高级技巧,它的用法格外的简单,只不过和前面所学的所有属性动画的知识不同,它并不是在3.0系统当中引入的,而是在3.1系统当中附增的一个新的功能。

textview.animate().x(500).y(500).setDuration(5000);

原文链接:http://www.apkbus.com/blog-705730-61891.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消