android ObjectAnimator基本应用

来源:互联网 发布:网络大电影题材 编辑:程序博客网 时间:2024/06/01 13:12

上篇博客讲过了ObjectAnimator是继承了ValueAnimator,所以ValueAnimator类方法ObjectAnimator类都可以使用,比如我只想让view在y轴方向平移,如果是纯用动画实现,ValueAnimator是做不了的,而ObjectAnimator就能实现针对View的一些属性进行动画操作,

比如让一个textview透明

ObjectAnimator animator = ObjectAnimator.ofFloat(tv_object_animator,"alpha",1,0,0.5f);animator.setDuration(2000);animator.start();

ofFloat()是ObjectAnimator类的静态方法,看下它的方法以及说明

/** * 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;}


第一个参数:target是指你应用动画到那个控件上

第二个参数:propertyName是属性的名称,就是你作用于那个view上的动画属性

第二个参数values:是一个可变参数,表示propertyName这个属性值的变化从哪变到哪


现在第一个和第三个值都好说,就是第二个值有时候不知道传啥,我刚开始以为这个属性值都是在ObjectAnimator类中,后来在源码中找了一下没有,然后想想它既然在textview上能起作用,而我们动画作用于view是任意的,所以就想到了这个属性值名称应该在view上,只要在view中找到和动画相关的set方法,把它的形参拷贝进去就行,

比如这样:

public void setTranslationX(float translationX) {    if (translationX != getTranslationX()) {        invalidateViewProperty(true, false);        mRenderNode.setTranslationX(translationX);        invalidateViewProperty(false, true);        invalidateParentIfNeededAndWasQuickRejected();        notifySubtreeAccessibilityStateChangedIfNeeded();    }}
只要把translationX当做上面的第二个参数就行,总结下view中常用的和动画有关的set方法

透明度

public void setAlpha(float alpha) {}
旋转

public void setRotation(float rotation) {}
public void setRotationX(float rotationX) {}
public void setRotationY(float rotationY) {}

平移

public void setTranslationX(float translationX) {}
public void setTranslationY(float translationY) {}
缩放

public void setScaleX(float scaleX) {}
public void setScaleY(float scaleY) {}






0 0