Skip to content

Instantly share code, notes, and snippets.

@alxscms
Created February 8, 2015 18:31
Show Gist options
  • Select an option

  • Save alxscms/1c262ebefbac6e10f8a0 to your computer and use it in GitHub Desktop.

Select an option

Save alxscms/1c262ebefbac6e10f8a0 to your computer and use it in GitHub Desktop.
Android expand & collapse animation
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class CollapseAnimation extends Animation implements Animation.AnimationListener {
private View mView;
private int mInitialHeight;
private boolean mAnimating;
public CollapseAnimation(View view) {
mView = view;
mAnimating = false;
this.setAnimationListener(this);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// we need this verification because applyTransformation gets called one or two
// times before onAnimationStart and the same happens after onAnimationEnd
if (mAnimating) {
mView.getLayoutParams().height = mInitialHeight - (int) (mInitialHeight * interpolatedTime);
mView.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
@Override
public void onAnimationStart(Animation animation) {
mInitialHeight = mView.getMeasuredHeight();
mAnimating = true;
}
@Override
public void onAnimationEnd(Animation animation) {
mAnimating = false;
mView.getLayoutParams().height = 0;
mView.requestLayout();
mView.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class ExpandAnimation extends Animation implements Animation.AnimationListener {
private View mView;
private int mInitialHeight;
private int mTargetHeight;
private boolean mAnimating;
public ExpandAnimation(View view) {
mView = view;
mAnimating = false;
this.setAnimationListener(this);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// we need this verification because applyTransformation gets called one or two
// times before onAnimationStart and the same happens after onAnimationEnd
if (mAnimating) {
mView.getLayoutParams().height = (int) ((mTargetHeight - mInitialHeight) * interpolatedTime) + mInitialHeight;
mView.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
@Override
public void onAnimationStart(Animation animation) {
mView.setVisibility(View.GONE);
mInitialHeight = mView.getHeight();
mView.measure(View.MeasureSpec.makeMeasureSpec(mView.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
mTargetHeight = mView.getMeasuredHeight();
mView.setVisibility(View.VISIBLE);
mAnimating = true;
}
@Override
public void onAnimationEnd(Animation animation) {
mAnimating = false;
mView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
mView.requestLayout();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment