Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between <objectAnimator> and ValueAnimator aka <animator>?

I started learning animations in android, reading https://developer.android.com/guide/topics/resources/animation-resource.html

found out there are two elements in xml and ValueAnimator aka

former one is used to animate object's properties but got confused with the definition the linked page provided. Which is :

"Performs an animation over a specified amount of time. Represents a ValueAnimator"

The both tags have same attributes:

    <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"]/>

Can anyone explain the difference and when to use what? Any answer and comment is appreciated.

like image 496
Arpit Chhabra Avatar asked Apr 06 '17 16:04

Arpit Chhabra


1 Answers

ObjectAnimator is a subclass of ValueAnimator. Main difference is that in case of ValueAnimator you have to override onAnimationUpdate(...) method where you will specify where to apply animated value:

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

ObjectAnimator will take care of this on its own:

ObjectAnimator.ofFloat(view, View.ALPHA, 0, 1).start();

In case of XML pay attention to "propertyName" of objectAnimator, which is not there for animator tag.

Also starting from API 23 you can also animate several properties at the same time:

<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>

and/or customize animation frames:

<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>
like image 104
George Keeper Avatar answered Oct 23 '22 06:10

George Keeper