Created
September 11, 2015 15:01
-
-
Save antonshkurenko/d24cd22660129bb0cb4c to your computer and use it in GitHub Desktop.
Util class to expand/collapse views with animations. Android.
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
package ua.com.i2i.futurum.utils; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.animation.Animation; | |
import android.view.animation.Transformation; | |
/** | |
* credits: http://stackoverflow.com/a/13381228/4142087 | |
* util class to expand/collapse view with animation | |
*/ | |
public class ViewExpander { | |
private ViewExpander() { | |
} | |
public static void expand(final View v) { | |
v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); | |
final int targetHeight = v.getMeasuredHeight(); | |
// Older versions of android (pre API 21) cancel animations for views with a height of 0. | |
v.getLayoutParams().height = 1; | |
v.setVisibility(View.VISIBLE); | |
Animation a = new Animation() | |
{ | |
@Override | |
protected void applyTransformation(float interpolatedTime, Transformation t) { | |
v.getLayoutParams().height = interpolatedTime == 1 | |
? ViewGroup.LayoutParams.WRAP_CONTENT | |
: (int)(targetHeight * interpolatedTime); | |
v.requestLayout(); | |
} | |
@Override | |
public boolean willChangeBounds() { | |
return true; | |
} | |
}; | |
// 1dp/ms | |
a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density)); | |
v.startAnimation(a); | |
} | |
public static 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/ms | |
a.setDuration((int)(initialHeight / 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