animate Text View Max Lines Change - Android User Interface

Android examples for User Interface:TextView Animation

Description

animate Text View Max Lines Change

Demo Code


//package com.java2s;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.view.View;
import android.widget.TextView;

public class Main {
    private static final String MAX_HEIGHT_ATTR = "maxHeight";

    public static void animateTextViewMaxLinesChange(
            final TextView textView, final int maxLines, final int duration) {
        final int startHeight = textView.getMeasuredHeight();
        textView.setMaxLines(maxLines);/* w  ww . ja  v a  2 s  .co m*/
        textView.measure(View.MeasureSpec.makeMeasureSpec(
                textView.getWidth(), View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(0,
                        View.MeasureSpec.UNSPECIFIED));
        final int endHeight = textView.getMeasuredHeight();
        ObjectAnimator animation = ObjectAnimator.ofInt(textView,
                MAX_HEIGHT_ATTR, startHeight, endHeight);
        animation.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (textView.getMaxHeight() == endHeight) {
                    textView.setMaxLines(maxLines);
                }
            }
        }

        );
        animation.setDuration(duration).start();
    }
}

Related Tutorials