Skip to content

Instantly share code, notes, and snippets.

@ikew0ng
Created July 27, 2014 09:37
Show Gist options
  • Save ikew0ng/1e93a31cd00a1b2f7a1f to your computer and use it in GitHub Desktop.
Save ikew0ng/1e93a31cd00a1b2f7a1f to your computer and use it in GitHub Desktop.
利用OnPreDrawListener监听绘制前的时机,在这个点控件已经完全ready(完成可measure, layout),此时可以获取到动画所必须的参数,也保证动画的流畅。
btnMenu.setVisibility(View.VISIBLE);
btnClose.setVisibility(View.VISIBLE);
btnRefresh.setVisibility(View.VISIBLE);
btnMenu.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
btnMenu.getViewTreeObserver().removeOnPreDrawListener(this);
ViewGroup.MarginLayoutParams params =
(ViewGroup.MarginLayoutParams) btnClose.getLayoutParams();
int marginBottom = params.bottomMargin;
int translationY = btnClose.getHeight() + marginBottom;
btnClose.setTranslationY(translationY);
btnRefresh.setTranslationY(translationY);
btnMenu.setTranslationY(translationY);
Interpolator interpolator = new OvershootInterpolator();
ObjectAnimator btnCloseAnimator =
ObjectAnimator.ofFloat(btnClose, "translationY", 0);
btnCloseAnimator.setDuration(ANIMATION_DURATION);
btnCloseAnimator.setInterpolator(interpolator);
ObjectAnimator btnRefreshAnimator =
ObjectAnimator.ofFloat(btnRefresh, "translationY", 0);
btnRefreshAnimator.setInterpolator(interpolator);
btnRefreshAnimator.setDuration(ANIMATION_DURATION);
ObjectAnimator btnMenuAnimator =
ObjectAnimator.ofFloat(btnMenu, "translationY", 0);
btnMenuAnimator.setInterpolator(interpolator);
btnMenuAnimator.setDuration(ANIMATION_DURATION);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(btnCloseAnimator);
animatorSet.play(btnRefreshAnimator).after(50);
animatorSet.play(btnMenuAnimator).after(100);
animatorSet.start();
return true;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment