flip TextView - Android User Interface

Android examples for User Interface:TextView

Description

flip TextView

Demo Code


import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class Main {
  private final static String TAG = "";

  public static void flipTextView(final TextView view, final String firstText,
      final String secondText) {
    if (view == null) {
      Log.e(TAG, "Attempting to make changes on a null view.");
      return;/*from   w ww .j a  v  a2s.c om*/
    }

    Context context = view.getContext();
    int duration = context.getResources().getInteger(
        android.R.integer.config_shortAnimTime) / 2;

    ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, "scaleX", 1.0f,
        0.0f);
    scaleDownX.setDuration(duration);

    ObjectAnimator scaleUpX = ObjectAnimator
        .ofFloat(view, "scaleX", 0.0f, 1.0f);
    scaleUpX.setDuration(duration);

    scaleDownX.addListener(new Animator.AnimatorListener() {
      @Override
      public void onAnimationStart(Animator animation) {

      }

      @Override
      public void onAnimationEnd(Animator animation) {
        view.setVisibility(View.INVISIBLE);
        view.setScaleX(1.0f);
        if (view.getText().toString().equals(firstText)) {
          view.setText(secondText);
        } else {
          view.setText(firstText);
        }
        view.setVisibility(View.VISIBLE);
      }

      @Override
      public void onAnimationCancel(Animator animation) {

      }

      @Override
      public void onAnimationRepeat(Animator animation) {

      }
    });

    AnimatorSet flip = new AnimatorSet();
    flip.playSequentially(scaleDownX, scaleUpX);

    flip.start();
  }
}

Related Tutorials