Skip to content

Instantly share code, notes, and snippets.

@liu7yong
Last active December 21, 2015 12:19
Show Gist options
  • Save liu7yong/6304579 to your computer and use it in GitHub Desktop.
Save liu7yong/6304579 to your computer and use it in GitHub Desktop.
An android animation that collapse or expand a View object.
/**
*
*/
package us.liuyong.widget;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Transformation;
/**
* @author liuyong
*
*/
public class DropDownAnimation extends Animation {
public static final int DOWN = 0;
public static final int UP = 1;
private int mMaxHeight;
private View mView;
private int mDirection;
public DropDownAnimation(View view, int maxHeight, int direction) {
mView = view;
mMaxHeight = maxHeight;
mDirection = direction;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newHeight;
if (mDirection == DOWN) {
newHeight = (int) (mMaxHeight * interpolatedTime);
} else {
newHeight = (int) (mMaxHeight * (1.0f - interpolatedTime));
}
LayoutParams params = mView.getLayoutParams();
params.height = newHeight;
mView.setLayoutParams(params);
}
}
@liu7yong
Copy link
Author

The usage is very simple:

DropDownAnimation anim = new DropDownAnimation(targetView, targetView
        .getMeasuredHeight(), DropDownAnimation.UP);
anim.setDuration(300);
targetView.startAnimation(anim);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment