上篇博客讲过了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 setName(), where name is * the value of the propertyName 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) {}






更多相关文章

  1. 【Android】ImageButton的记录
  2. Android两行代码搞定ViewPager的过渡动画
  3. Android(安卓)搜索框:SearchView 的属性和用法详解
  4. Android的振动服务,vibrator第二个参数的真正含义
  5. Android之LayoutInflater了解(20190109)
  6. Activity切换动画效果的修改
  7. Android支持RTL(从右向左)语言
  8. 进程常驻
  9. Android(安卓)Manifest.xml文件解析

随机推荐

  1. Android Looper&Handler 源码
  2. android序列化与反序列话HashMap到sqlite
  3. android:设计一个具有3个选项的菜单程序,
  4. 【Android】Suggestion: use tools:overr
  5. SDK无法下载更新的问题
  6. [Android]可自定义显示时长的Toast
  7. Android通过网络URL获取图片并显示
  8. android post方式传递参数并获取返回数据
  9. 解决安卓9.0版本不能访问http请求问题
  10. Android底部弹出的popupWindow