/** * Constructs and returns an ObjectAnimator that animates between float values. A single * value implies that that value is the one being animated to. Two values imply starting * and ending values. More than two values imply a starting value, values to animate through * along the way, and an ending value (these values will be distributed evenly across * the duration of the animation). * * @param target The object whose property is to be animated. This object should * have a public method on it called <code>setName()</code>, where <code>name</code> is * the value of the <code>propertyName</code> parameter. * @param propertyName The name of the property being animated. * @param values A set of values that the animation will animate between over time. * @return An ObjectAnimator object that is set up to animate between the given values. */ public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) { ObjectAnimator anim = new ObjectAnimator(target, propertyName); anim.setFloatValues(values); return anim; }
中文翻译:
构建并返回一个objectanimator动画之间的浮点值。一个单一的值意味着这个值是一个被动画。双值意味着开始和结束值。超过2个值意味着一个起始值,通过一个方向动画的值,和一个结束值(这些值将均匀地分布在动画的持续时间)。
简单讲就是设置属性与属性的变化值
如:
ObjectAnimator.ofFloat(example,"translationY",0,example.getHeight()).start();
实现view
从y:0->view的高度 这样一个相对于view 直线向下移动动画
又如旋转动画:
ObjectAnimator.ofFloat(example,"rotation",0,360).setDuration(3000).start();
下面我们来看一看xml方式的创建:
1:需要在res文件下面创建一个animator文件夹
2:然后创建Animator resouce files
3:加载xml动画 系统提供一个类 AnimatorInflator 这个类的静态方法loadAnimator可以实现加载xml动画
4:绑定你需要展现的view身上,animator.setTarget(view);
Animator animatorr = AnimatorInflater.loadAnimator(this, R.animator.animator_rotation); animatorr.setTarget(example); animatorr.start();xml的样板:<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:propertyName="rotation" android:repeatMode="reverse" android:valueFrom="0" android:valueTo="360" />
在3.0之前有组合动画,属性动画业同样有:
代码方式:
AnimatorSet 是一个非常有意思的类,可以实现动画的组合
主要API有:playTogether(Animator... items) 一起同时执行多个动画playSequentially(Animator... items) 顺序执行多个动画,一个接一个如:ObjectAnimator trans = ObjectAnimator.ofFloat(example, "translationY", 0, example.getHeight()).setDuration(3000); ObjectAnimator alpha = ObjectAnimator.ofFloat(example, "alpha", 1, 0).setDuration(3000); AnimatorSet set = new AnimatorSet(); set.playTogether(trans,alpha); set.start();实现的是移动并消失动画xml方式:<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="together"> <objectAnimator android:duration="3000" android:propertyName="translationY" android:valueFrom="0f" android:valueTo="200f"/> <objectAnimator android:duration="3000" android:propertyName="alpha" android:valueFrom="0.0f" android:valueTo="1.0f" /> </set>