Created
January 9, 2014 10:10
-
-
Save laaptu/8332048 to your computer and use it in GitHub Desktop.
Expand Collapse Animation of a view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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