Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.IntEvaluator;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;
* Created by Bernat on 17/12/2014.
public class ViewUtils {
private int size;
public ViewUtils(int size) {
this.size = size;
public void expand(View summary) {
//set Visible
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
summary.measure(widthSpec, size);
ValueAnimator valueAnimator = slideAnimator(0, size, summary);
public void collapse(final View summary) {
int finalHeight = summary.getHeight();
ValueAnimator mAnimator = slideAnimator(finalHeight, 0, summary);
mAnimator.addListener(new Animator.AnimatorListener() {
public void onAnimationEnd(Animator animator) {
//Height=0, but it set visibility to GONE
public void onAnimationStart(Animator animator) {
public void onAnimationCancel(Animator animator) {
public void onAnimationRepeat(Animator animator) {
private ValueAnimator slideAnimator(int start, int end, final View summary) {
ValueAnimator heightAnimator = ObjectAnimator.ofInt(start, end);
heightAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
heightAnimator.setEvaluator(new IntEvaluator());
heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator valueAnimator) {
//Update Height
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = summary.getLayoutParams();
layoutParams.height = value;
return heightAnimator;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment