Android ObjectAnimator


Android ObjectAnimator Tutorial and Examples.

ObjectAnimator is a class that allows properties on target objects. This class derives from android.animation.ValueAnimator.

That ValueAnimator is responsible for provision a simple timing engine for running animations which calculate animated values and set them on target objects.

Through the constructors of our ObjectAnimator, we pass the target object that need to be animated. We also pass the name of the property to be animated.

ObjectAnimator API Definition

ObjectAnimator derives from the ValueAnimator class. Both reside in the android.animation package:

public final class ObjectAnimator 
extends ValueAnimator 

Here's it's inheritance tee:

java.lang.Object
   ↳    android.animation.Animator
       ↳    android.animation.ValueAnimator
           ↳    android.animation.ObjectAnimator

Setting Animations

You can set animations in both code and static xml resource. Here's an example of setting it using xml:

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:valueTo="200"
    android:valueType="floatType"
    android:propertyName="y"
    android:repeatCount="1"
    android:repeatMode="reverse"/>

To see how to set in code, proceed to the examples below.

Quick ObjectAnimator Examples

1. How to Fade in and Fade Out a View using ObjectAnimator

Our aim is to create methods that allow us fade in and fade out.

First let's create an interface AnimationListener:

public interface AnimationListener {

    /**
     * We need to make our View visible
     * before fade in animation starts
     */
    interface OnAnimationStartListener{
        void onAnimationStart();
    }

    /**
     * We need to make View invisible
     * after fade out animation ends.
     */
    interface OnAnimationEndListener{
        void onAnimationEnd();
    }

}

You can see that's an interface with two method signatures: onAnimationStart() and onAnimationEnd().

Then here's the method that fade in a view:

    /** 
     * View will appear on screen with 
     * fade in animation. Notifies onAnimationStartListener 
     * when fade in animation is about to start. 
     * 
     * @param view 
     * @param duration 
     * @param onAnimationStartListener 
     */ 
    public static void animateFadeIn(View view, long duration, final AnimationListener.OnAnimationStartListener onAnimationStartListener) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
        objectAnimator.setDuration(duration);
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override 
            public void onAnimationStart(Animator animation) {
                if (onAnimationStartListener != null)
                    onAnimationStartListener.onAnimationStart();
            } 

            @Override 
            public void onAnimationEnd(Animator animation) {

            } 

            @Override 
            public void onAnimationCancel(Animator animation) {

            } 

            @Override 
            public void onAnimationRepeat(Animator animation) {

            } 
        }); 
        objectAnimator.start();
    } 

We've initialized the ObjectAnimator by invoking the static ofFloat() method. We then set the duration, a long which we received via our method as a parameter. To set the duration we've used the setDuration() method.

Then added our AnimatorListener, where we invoke our custom AnimationListener.onAnimationStart() method inside the onAnimationStart() method of the android.animation.Animator class.

Then what about fading out?

well agan we use ObjectAnimator class. The difference is that this time we are invoking the onAnimationEnd() from our customAnimationListener interface and do it inside the Animator.OnAnimationEnd() from the android.animation package.

/** 
     * View will disappear from screen with 
     * fade out animation. Notifies onAnimationEndListener 
     * when fade out animation is ended. 
     * 
     * @param view 
     * @param duration 
     * @param onAnimationEndListener 
     */ 
    public static void animateFadeOut(View view, long duration, final AnimationListener.OnAnimationEndListener onAnimationEndListener) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "alpha", 1, 0);
        objectAnimator.setDuration(duration);
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override 
            public void onAnimationStart(Animator animation) {

            } 

            @Override 
            public void onAnimationEnd(Animator animation) {
                if (onAnimationEndListener != null)
                    onAnimationEndListener.onAnimationEnd();
            } 

            @Override 
            public void onAnimationCancel(Animator animation) {

            } 

            @Override 
            public void onAnimationRepeat(Animator animation) {

            } 
        }); 
        objectAnimator.start();
    } 

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post