如果需要使用XML方式定义补间动画
首先, 在资源路径 res 下创建目录:animator(名字不能错)
其次, 在 res/animator 目录下创建xml文件,根节点必须是: 。如果是组合动画,根节点必须是: 。(具体可以参考文章中的代码示例)

propertyName: alpha

1. 代码示例 (代码中设置属性)

//不透明-完全透明-半透明(会停留在最后的设置效果上)
ObjectAnimator alpha = ObjectAnimator.ofFloat(ivView, "alpha", 1, 0, 0.5f);
alpha.setDuration(2000);
alpha.start();
//上下代码等效
//        ObjectAnimator oa = new ObjectAnimator();
//        oa.setTarget(ivView);
//        oa.setPropertyName("alpha");
//        oa.setFloatValues(1, 0, 0.5f);
//        oa.setDuration(2000);
//        oa.start();

2. 代码示例(XML中设置属性)

  • R.animator.animator_my_alpha
  • <?xml version="1.0" encoding="utf-8"?>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                    android:duration="2000"
                    android:propertyName="alpha"
                    android:repeatCount="1"
                    android:valueFrom="1"
                    android:valueTo="0.5f"
                    android:repeatMode="reverse">
    </objectAnimator>
    <!-- 外层可以使用 animator 包裹-->
    <!-- 外层也可以不使用 animator 包裹-->
    <!--<animator xmlns:android="http://schemas.android.com/apk/res/android">-->
    <!--<objectAnimator-->
            <!--android:duration="2000"-->
            <!--android:propertyName="alpha"-->
            <!--android:repeatCount="1"-->
            <!--android:valueFrom="1"-->
            <!--android:valueTo="0.5f"-->
            <!--android:repeatMode="reverse">-->
    <!--</objectAnimator>-->
    <!--</animator>-->
    
  • 调用XML
  • ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator_my_alpha);
    animator.setTarget(ivView);
    animator.start();
    

    propertyName:rotation(绕中心旋转)、rotationX(绕X轴旋转)、rotationY(绕Y轴旋转)

    1. 代码示例(代码中设置属性)

    ObjectAnimator rotationX = ObjectAnimator.ofFloat(ivView, "rotation", 0, 180, 90, 360);
    rotationX.setDuration(2000);
    rotationX.start();
    

    2. 代码示例(XML中设置属性)

  • R.animator.animator_my_rotation
  • <?xml version="1.0" encoding="utf-8"?>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                    android:duration="2000"
                    android:propertyName="rotation"
                    android:repeatCount="2"
                    android:repeatMode="reverse"
                    android:valueFrom="0"
                    android:valueTo="180">
    </objectAnimator>
    
  • 调用XML
  • ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator_my_rotation);
    animator.setTarget(ivView);
    animator.start();
    

    propertyName:translationX(X轴方向)、translationY(Y轴方向)

    1. 代码示例(代码中设置属性)

    ObjectAnimator translationY = ObjectAnimator.ofFloat(ivView, "translationY", 0, 100, 0, 100);
    translationY.setDuration(2000);
    translationY.start();
    

    2. 代码示例(XML中设置属性)

  • R.animator.animator_my_translation_y
  • <?xml version="1.0" encoding="utf-8"?>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                    android:duration="2000"
                    android:propertyName="translationX"
                    android:repeatCount="2"
                    android:repeatMode="reverse"
                    android:valueFrom="0"
                    android:valueTo="200">
    </objectAnimator>
    
  • 调用XML
  • ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator_my_translation_y);
    animator.setTarget(ivView);
    animator.start();
    

    propertyName:scaleX(X轴方向)、scaleY(Y轴方向)

    1. 代码示例(代码中设置属性)

    ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivView, "scaleY", 1, 0.5f, 1, 0.5f);
    scaleY.setDuration(2000);
    scaleY.start();
    

    2. 代码示例(XML中设置属性)

  • R.animator.animator_my_scale_y
  • <?xml version="1.0" encoding="utf-8"?>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                    android:duration="2000"
                    android:propertyName="scaleY"
                    android:repeatCount="2"
                    android:repeatMode="reverse"
                    android:valueTo="2"
                    android:valueFrom="1">
    </objectAnimator>
    
  • 调用XML
  • ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator_my_scale_y);
    animator.setTarget(ivView);
    animator.start();
    

    playSequentially:依次执行所有的动画
    playTogether:所有的动画一起执行
    1. 代码示例(代码中设置属性)

    AnimatorSet set = new AnimatorSet();
    ObjectAnimator alpha = ObjectAnimator.ofFloat(ivView, "alpha", 1, 0.5f, 1);
    ObjectAnimator rotation = ObjectAnimator.ofFloat(ivView, "rotation", 0, 180, 90, 360);
    ObjectAnimator translationY = ObjectAnimator.ofFloat(ivView, "translationY", 0, 100, 0, 100);
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivView, "scaleY", 1, 2, 1, 0.5f);
    set.setDuration(3000);
    //playSequentially:依次执行所有的动画
    set.playSequentially(alpha, rotation, translationY, scaleY);
    //playTogether:把所有的动画一起执行
    //set.playTogether(alpha, rotationX, translationY, scaleY);
    set.start(); //切记:要start
    

    2. 代码示例(XML中设置属性)

  • R.animator.animator_my_set1
  • <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:ordering="together">
        <objectAnimator
            android:duration="2000"
            android:propertyName="translationX"
            android:repeatCount="2"
            android:repeatMode="reverse"
            android:valueFrom="0"
            android:valueTo="200" />
        <objectAnimator
            android:duration="2000"
            android:propertyName="translationY"
            android:repeatCount="2"
            android:repeatMode="reverse"
            android:valueFrom="0"
            android:valueTo="200" />
    
  • R.animator.animator_my_set2
  • <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:ordering="together">
        <set android:ordering="sequentially">
            <objectAnimator
                    android:duration="1000"
                    android:propertyName="translationX"
                    android:repeatCount="2"
                    android:repeatMode="reverse"
                    android:valueFrom="0"
                    android:valueTo="200"/>
            <objectAnimator
                    android:duration="1000"
                    android:propertyName="translationY"
                    android:repeatCount="2"
                    android:repeatMode="reverse"
                    android:valueFrom="0"
                    android:valueTo="200"/>
        <objectAnimator
                android:duration="2000"
                android:propertyName="rotationX"
                android:repeatCount="2"
                android:repeatMode="reverse"
                android:valueFrom="0"
                android:valueTo="180"/>
    
  • 调用XML
  • //AnimatorSet animator = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.animator_my_set1);
    AnimatorSet animator = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.animator_my_set2);
    animator.setTarget(ivView);
    animator.start();
    补间动画:请点击此处

    帧动画:请点击此处