天天看点

属性动画 进阶(ObjectAnimator)首先正文谢谢

首先

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

正文

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

ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);  
animator.setDuration();  
animator.start();  
...
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f);  
animator.setDuration();  
animator.start();  
...
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationX", 0f, 360f);  
animator.setDuration();  
animator.start();  
...
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationY", 0f, 360f);  
animator.setDuration();  
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, f, f);

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

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

//移动xy
Path path = new Path();
     path.lineTo(, );
     path.lineTo(, );
     //path.lineTo(,);
     //path.lineTo(,);
     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(, );
      path.lineTo(, );
      path.lineTo(, );
      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!欢淫吐槽,指正,评论!

继续阅读