animate Text Change for TextView - Android User Interface

Android examples for User Interface:TextView Animation

Description

animate Text Change for TextView

Demo Code


//package com.java2s;
import android.animation.Animator;

import android.animation.ObjectAnimator;

import android.support.annotation.IdRes;

import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.TextView;

public class Main {
    public static final int DURATION_SHORT = 300;

    public static void animateTextChange(final TextView view,
            @IdRes final int toText, final Runnable rWhenEnd) {
        ObjectAnimator alpha = ObjectAnimator
                .ofFloat(view, "alpha", 1f, 0f);
        final ObjectAnimator restore = ObjectAnimator.ofFloat(view,
                "alpha", 0f, 1f);
        alpha.setDuration(DURATION_SHORT);
        alpha.setInterpolator(new AccelerateDecelerateInterpolator());
        restore.setDuration(DURATION_SHORT);
        restore.setInterpolator(new AccelerateDecelerateInterpolator());
        alpha.addListener(new Animator.AnimatorListener() {
            @Override/* w w  w  . j  av  a  2  s.  co m*/
            public void onAnimationStart(Animator animation) {
                // Do nothing.
            }

            @SuppressWarnings("ResourceType")
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setText(toText);
                restore.start();
            }

            @SuppressWarnings("ResourceType")
            @Override
            public void onAnimationCancel(Animator animation) {
                view.setText(toText);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                // Do nothing.
            }
        });
        if (rWhenEnd != null)
            restore.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    rWhenEnd.run();
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    rWhenEnd.run();
                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
        alpha.start();
    }

    public static void animateTextChange(final TextView view,
            final String toText) {
        ObjectAnimator alpha = ObjectAnimator
                .ofFloat(view, "alpha", 1f, 0f);
        final ObjectAnimator restore = ObjectAnimator.ofFloat(view,
                "alpha", 0f, 1f);
        alpha.setDuration(DURATION_SHORT);
        alpha.setInterpolator(new AccelerateDecelerateInterpolator());
        restore.setDuration(DURATION_SHORT);
        restore.setInterpolator(new AccelerateDecelerateInterpolator());
        alpha.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                // Do nothing.
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.setText(toText);
                restore.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                view.setText(toText);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                // Do nothing.
            }
        });
        alpha.start();
    }
}

Related Tutorials