属性动画 进阶(ObjectAnimator)

首先

这不是一篇介绍属性动画使用的文章,如何使用网上一大把,不愿意做别人做过的事情。

正文

我在看了N多介绍属性动画的文章后,发现所有千篇一律,诸如以下代码(请关注第二个参数属性名)

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

我很疑惑的是,难道我大google设计API,设计成这样?需要开发者记住每一个属性名吗,需要实现一个动画的时候,还需要先去想,这个属性的全拼是怎么样的,实在太扯淡。

观察ObjectAnimator方法之后发现以下方法


ofInt(T target, Property<T, Integer> property, int... values)

ofInt(T target, Property<T, Integer> xProperty,Property<T, Integer> yProperty, Path path)

ofFloat(T target, Property<T, Float> property,
            float... values)

ofFloat(T target, Property<T, Float> xProperty,Property<T, Float> yProperty, Path path)

...

其中第二个带xy属性的方法,Api21以上才有,使用起来veryEasy

//旋转
ObjectAnimator animation = ObjectAnimator.ofFloat(fabIconStar, View.ROTATION, 0f, 45f);

//ObjectAnimator animation = ObjectAnimator.ofFloat(fabIconStar,View.ROTATION, 0f, 45f,0f);

//转回去
ObjectAnimator animation = ObjectAnimator.ofFloat(tager, View.ROTATION, 45f, 0);

//移动xy
Path path = new Path();
     path.lineTo(10, 10);
     path.lineTo(50, 20);
     //path.lineTo(10,10);
     //path.lineTo(0,0);
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
         ObjectAnimator animation1 = ObjectAnimator.ofFloat(tager, View.TRANSLATION_X,View.TRANSLATION_Y, path);
         animation1.start();
     }

//移动回去
 Path path = new Path();
      path.moveTo(50, 20);
      path.lineTo(10, 10);
      path.lineTo(0, 0);
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
          ObjectAnimator animation1 = ObjectAnimator.ofFloat(tager, View.TRANSLATION_X,View.TRANSLATION_Y, path);
          animation1.start();
       }

可使用属性View内所有 Property 属性,

 Property<View, Float> 
 ...

有兴趣的可以一个个去尝试一下,在这里不作详述了。

谢谢

是不是soEasy!欢淫吐槽,指正,评论!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值