Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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);
        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();
    }
}