Skip to content

Instantly share code, notes, and snippets.

@laaptu
Created January 9, 2014 10:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laaptu/8332048 to your computer and use it in GitHub Desktop.
Save laaptu/8332048 to your computer and use it in GitHub Desktop.
Expand Collapse Animation of a view
public void collapse(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = initialHeight
- (int) (initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp per milliseconds
a.setDuration((int) (initialHeight / v.getContext().getResources()
.getDisplayMetrics().density));
v.startAnimation(a);
}
public void expand(final View v) {
v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final int measuredHeight = v.getMeasuredHeight();
v.getLayoutParams().height = 0;
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1 ? LayoutParams.WRAP_CONTENT
: (int) (measuredHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp per milliseconds
a.setDuration((int) (measuredHeight / v.getContext().getResources()
.getDisplayMetrics().density));
v.startAnimation(a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment