Skip to content

Instantly share code, notes, and snippets.

@MFlisar
Created January 29, 2015 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MFlisar/738dfa03a1579cc7338a to your computer and use it in GitHub Desktop.
Save MFlisar/738dfa03a1579cc7338a to your computer and use it in GitHub Desktop.
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