Android动画--属性动画

1. 常用类

  • ObjectAnimator
  • ValueAnimator (数值发生器)
  • AnimatorSet (组合动画)

ObjectAnimator. ofFloat()源码解析

    ObjectAnimator#ofFloat
    public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
        ObjectAnimator anim = new ObjectAnimator(target, propertyName);
        anim.setFloatValues(values);
        return anim;
    }

参数解析

  1. target需要做动画的目标View
  2. propertyName需要目标View的什么属性做变更
  3. float... values 属性变化的取值过程

使用注意事项:使用时propertyName一定要有对应的get和set方法。因为其内部原理就是通过Java反射机制来调用set方法修改对象的属性值。

2. 属性值

  1. 平移
    • translationX(横坐标平移)
    • translationY(纵坐标平移)
      代码示例
    ObjectAnimator.ofFloat(mAnimationView,"translationX",200)
                    .setDuration(1500)
                    .start();
    
    ObjectAnimator.ofFloat(mAnimationView,"translationY",360)
                    .setDuration(1500)
                    .start();
    
  2. 旋转
    • rotation(根据一个点旋转)
    • rotationX(根据一个X轴旋转)
    • rotationY(根据一个Y轴旋转)
      代码示例
    ObjectAnimator.ofFloat(mAnimationView,"rotation",360,0)
                    .setDuration(1500)
                    .start();
    
    ObjectAnimator.ofFloat(mAnimationView,"rotationX",360,0)
                    .setDuration(1500)
                    .start();
    
    ObjectAnimator.ofFloat(mAnimationView,"rotationY",360)
                    .setDuration(1500)
                    .start();
    
  3. 缩放
    • scaleX(横向缩放)
    • scaleY(纵向缩放)
      代码示例
    ObjectAnimator.ofFloat(mAnimationView,"scaleX",0.25f,1)
                    .setDuration(3000)
                    .start();
    
    ObjectAnimator.ofFloat(mAnimationView,"scaleY",0.25f,1)
                    .setDuration(3000)
                    .start();
    
  4. 透明度
    • alpha
      代码示例
    ObjectAnimator.ofFloat(mAnimationView,"alpha",0.25f,1)
                    .setDuration(3000)
                    .start();
    

3. 组合动画AnimatorSet

代码示例

            Animator animator1 = ObjectAnimator.ofFloat(mAnimationView,"scaleX",0.25f,1)
                    .setDuration(3000);
            Animator animator2 = ObjectAnimator.ofFloat(mAnimationView,"scaleY",0.25f,1)
                    .setDuration(3000);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.play(animator1).with(animator2);
            animatorSet.start();

AnimatorSet.play(animator)

    public Builder play(Animator anim) {
        if (anim != null) {
            return new Builder(anim);
        }
        return null;
    }

会生成一个AnimatorSet的内部类Builder。
Builder内有四个常用方法:

  1. after(Animator)执行完此动画后再执行之前插入的动画
  2. after(long delay)执行完此动画后延迟指定毫秒再执行之前插入的动画
  3. before(Animator)将现有动画插入到传入的动画之前执行
  4. with(Animator)将现有动画和传入的动画一同执行。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。