Created
September 16, 2014 05:13
-
-
Save akbarsha03/ef5ac0b63b09fe22a8f5 to your computer and use it in GitHub Desktop.
Expand/Collapse animation
This file contains hidden or 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 static Animation expand(final View v, final boolean expand) { | |
try { | |
Method m = v.getClass().getDeclaredMethod("onMeasure", int.class, int.class); | |
m.setAccessible(true); | |
m.invoke( | |
v, | |
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), | |
MeasureSpec.makeMeasureSpec(((View)v.getParent()).getMeasuredWidth(), MeasureSpec.AT_MOST) | |
); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
final int initialHeight = v.getMeasuredHeight(); | |
if (expand) { | |
v.getLayoutParams().height = 0; | |
} | |
else { | |
v.getLayoutParams().height = initialHeight; | |
} | |
v.setVisibility(View.VISIBLE); | |
Animation a = new Animation() { | |
@Override | |
protected void applyTransformation(float interpolatedTime, Transformation t) { | |
int newHeight = 0; | |
if (expand) { | |
newHeight = (int) (initialHeight * interpolatedTime); | |
} else { | |
newHeight = (int) (initialHeight * (1 - interpolatedTime)); | |
} | |
v.getLayoutParams().height = newHeight; | |
v.requestLayout(); | |
if (interpolatedTime == 1 && !expand) | |
v.setVisibility(View.GONE); | |
} | |
@Override | |
public boolean willChangeBounds() { | |
return true; | |
} | |
}; | |
a.setDuration(SPEED_ANIMATION_TRANSITION); | |
return a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment