-
-
Save LOG-TAG/444c41ba22e57f874f57 to your computer and use it in GitHub Desktop.
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
import android.view.View; | |
import android.view.ViewGroup.LayoutParams; | |
import com.nineoldandroids.animation.Animator; | |
import com.nineoldandroids.animation.AnimatorListenerAdapter; | |
import com.nineoldandroids.animation.ValueAnimator; | |
public class ExpandUtils | |
{ | |
public interface IOnAnimatorFinished | |
{ | |
public void onAnimatorFinished(); | |
} | |
public static void expand(final View v, final IOnAnimatorFinished animatorFinishedListener, Integer durationInMs) | |
{ | |
v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); | |
final int startHeight = 0; | |
final int targetHeight = v.getMeasuredHeight(); | |
v.getLayoutParams().height = startHeight; | |
v.setVisibility(View.VISIBLE); | |
ValueAnimator animation = ValueAnimator.ofInt(startHeight, targetHeight); | |
animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() | |
{ | |
@Override | |
public void onAnimationUpdate(ValueAnimator valueAnimator) | |
{ | |
v.getLayoutParams().height = (Integer) valueAnimator.getAnimatedValue(); | |
v.requestLayout(); | |
} | |
}); | |
animation.addListener(new AnimatorListenerAdapter() | |
{ | |
@Override | |
public void onAnimationEnd(Animator animation) | |
{ | |
v.getLayoutParams().height = LayoutParams.WRAP_CONTENT; | |
if (animatorFinishedListener != null) | |
animatorFinishedListener.onAnimatorFinished(); | |
} | |
}); | |
// 1dp/ms | |
animation.setDuration(durationInMs != null ? durationInMs : (int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density)); | |
animation.start(); | |
} | |
public static void collapse(final View v, final IOnAnimatorFinished animatorFinishedListener, Integer durationInMs) | |
{ | |
final int startHeight = v.getMeasuredHeight(); | |
final int targetHeight = 0; | |
ValueAnimator animation = ValueAnimator.ofInt(startHeight, targetHeight); | |
animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() | |
{ | |
@Override | |
public void onAnimationUpdate(ValueAnimator valueAnimator) | |
{ | |
v.getLayoutParams().height = (Integer) valueAnimator.getAnimatedValue(); | |
v.requestLayout(); | |
} | |
}); | |
animation.addListener(new AnimatorListenerAdapter() | |
{ | |
@Override | |
public void onAnimationEnd(Animator animation) | |
{ | |
v.setVisibility(View.GONE); | |
if (animatorFinishedListener != null) | |
animatorFinishedListener.onAnimatorFinished(); | |
} | |
}); | |
// 1dp/ms | |
animation.setDuration(durationInMs != null ? durationInMs : (int) (startHeight / v.getContext().getResources().getDisplayMetrics().density)); | |
animation.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment