您的位置:首页 > 移动开发 > Objective-C

属性动画 进阶(ObjectAnimator)

2017-02-27 14:49 162 查看

首先

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

正文

我在看了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!欢淫吐槽,指正,评论!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息