Skip to content

Instantly share code, notes, and snippets.

Embed
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) {
summary.setVisibility(View.VISIBLE);
//set Visible
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
summary.measure(widthSpec, size);
ValueAnimator valueAnimator = slideAnimator(0, size, summary);
valueAnimator.start();
}
public void collapse(final View summary) {
int finalHeight = summary.getHeight();
ValueAnimator mAnimator = slideAnimator(finalHeight, 0, summary);
mAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animator) {
//Height=0, but it set visibility to GONE
summary.setVisibility(View.GONE);
}
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
mAnimator.start();
}
private ValueAnimator slideAnimator(int start, int end, final View summary) {
ValueAnimator heightAnimator = ObjectAnimator.ofInt(start, end);
heightAnimator.setDuration(200);
heightAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
heightAnimator.setEvaluator(new IntEvaluator());
heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
//Update Height
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = summary.getLayoutParams();
layoutParams.height = value;
summary.setLayoutParams(layoutParams);
}
});
return heightAnimator;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment