做项目的时候经常遇到一些小动画的需求 所以决定整合一个工具类方便以后直接调用 先简单放一些项目用到的 以后有时间再更新。
public class AnimationUtil { private boolean ismHiddenActionstart = false; private static AnimationUtil mInstance; public static AnimationUtil with() { if (mInstance == null) { synchronized (AnimationUtil.class) { if (mInstance == null) { mInstance = new AnimationUtil(); } } } return mInstance; } /** * 从控件所在位置移动到控件的底部(隐藏) * * @param v * @param Duration 动画时间 * @param type 1:平移 2:旋转 3:渐显 * LinearInterpolator为匀速效果,Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果 */ public void moveToViewBottom(final View v, long Duration,int type) { if (v.getVisibility() != View.VISIBLE) return; if (ismHiddenActionstart) return; Animation animation = null; switch (type){ case 1: animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f); break; case 2: animation = new RotateAnimation(0f, 90f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f); break; case 3: animation = new AlphaAnimation(0.1f, 0.0f); animation.setFillAfter(false);//设置最后的动画效果 break; } assert animation != null; animation.setDuration(Duration); animation.setInterpolator(new AccelerateInterpolator()); v.clearAnimation(); v.setAnimation(animation); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { ismHiddenActionstart = true; } @Override public void onAnimationEnd(Animation animation) { v.setVisibility(View.GONE); ismHiddenActionstart = false; } @Override public void onAnimationRepeat(Animation animation) { } }); }