【Android 动画】动画详解之属性动画(五)

在前几篇中,我们了解了补间动画、插值器和属性动画中的ValueAnimator,这一篇,我们来了解下属性动画中的ObjectAnimator

ObjectAnimator是通过指定属性所对应的set方法来改变的。比如,我们上面指定的改变rotation的属性值, 在做动画时就会到指定控件(TextView)中去找对应的setRotation()方法来改变控件中对应的值。 在View中有关动画,总共有下面几组set方法:

         //1、透明度:alpha
         public void setAlpha(float alpha)

         //2、旋转度数:rotation、rotationX、rotationY
         public void setRotation(float rotation)
         public void setRotationX(float rotationX)
         public void setRotationY(float rotationY)

         //3、平移:translationX、translationY
         public void setTranslationX(float translationX)
         public void setTranslationY(float translationY)

         //缩放:scaleX、scaleY
         public void setScaleX(float scaleX)
         public void setScaleY(float scaleY)
复制代码

rotationX

  objectAnimator = ObjectAnimator.ofFloat(tvDemo, "rotationX", 0, 360, 0);
                objectAnimator.setDuration(2000);
                objectAnimator.start();
复制代码

20181224_163057.gif
alpha

  objectAnimator = ObjectAnimator.ofFloat(tvDemo, "alpha", 0, 1, 0);
                objectAnimator.setDuration(2000);
                objectAnimator.start();
复制代码

20181224_163125.gif
translationX

 objectAnimator = ObjectAnimator.ofFloat(tvDemo, "translationX", 0, 200, 0);
                objectAnimator.setDuration(2000);
                objectAnimator.start();
复制代码

20181224_163205.gif
scaleX

 objectAnimator = ObjectAnimator.ofFloat(tvDemo, "scaleX", 0, 3, 1);
                objectAnimator.setDuration(2000);
                objectAnimator.start();
复制代码

20181224_163226.gif

扩展:

上面使用View自带的set函数所对应属性的方法,如果以上方法满足不了我们的需求怎么办? 我们只需要在我们的view中定义一个set函数,与我们自定义的属性相对应即可。

最后献上源码 github

参考资料:自定义控件三部曲之动画篇

你的认可,是我坚持更新博客的动力,如果觉得有用,就请点个赞,谢谢

猜你喜欢

转载自juejin.im/post/5c74e196e51d4536ee3380e1