<objectAnimator>和ValueAnimator又名<animator>之间的区别?

Arp*_*bra 2 xml animation android

我开始在android中学习动画,阅读https://developer.android.com/guide/topics/resources/animation-resource.html

发现xml和ValueAnimator中有两个元素

前者用于动画对象的属性,但与链接页面提供的定义混淆.这是:

"在指定的时间内执行动画.表示ValueAnimator"

这两个标签具有相同的属性:

    <objectAnimator
    android:propertyName="string"
    android:duration="int"
    android:valueFrom="float | int | color"
    android:valueTo="float | int | color"
    android:startOffset="int"
    android:repeatCount="int"
    android:repeatMode=["repeat" | "reverse"]
    android:valueType=["intType" | "floatType"]/>

<animator
    android:duration="int"
    android:valueFrom="float | int | color"
    android:valueTo="float | int | color"
    android:startOffset="int"
    android:repeatCount="int"
    android:repeatMode=["repeat" | "reverse"]
    android:valueType=["intType" | "floatType"]/>
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释差异以及何时使用什么?任何答案和评论表示赞赏.

Geo*_*per 15

ObjectAnimator是ValueAnimator的子类.主要区别在于,在ValueAnimator的情况下,您必须覆盖onAnimationUpdate(...)方法,您将在其中指定应用动画值的位置:

ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
   @Override
   public void onAnimationUpdate(ValueAnimator animation) {
       view.setAlpha((Float) animation.getAnimatedValue());
   }
});
animator.start();
Run Code Online (Sandbox Code Playgroud)

ObjectAnimator将自行处理:

ObjectAnimator.ofFloat(view, View.ALPHA, 0, 1).start();
Run Code Online (Sandbox Code Playgroud)

如果是XML,请注意objectAnimator的"propertyName",而不是动画标签.

同样从API 23开始,您还可以同时为多个属性设置动画:

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                android:duration="1000"
                android:repeatCount="1"
                android:repeatMode="reverse">
    <propertyValuesHolder android:propertyName="x" android:valueTo="400"/>
    <propertyValuesHolder android:propertyName="y" android:valueTo="200"/>
</objectAnimator>
Run Code Online (Sandbox Code Playgroud)

和/或自定义动画帧:

<animator xmlns:android="http://schemas.android.com/apk/res/android"
          android:duration="1000"
          android:repeatCount="1"
          android:repeatMode="reverse">
    <propertyValuesHolder>
        <keyframe android:fraction="0" android:value="1"/>
        <keyframe android:fraction=".2" android:value=".4"/>
        <keyframe android:fraction="1" android:value="0"/>
    </propertyValuesHolder>
</animator>
Run Code Online (Sandbox Code Playgroud)