Android动画工具类(平移 渐显 旋转)

做项目的时候经常遇到一些小动画的需求 所以决定整合一个工具类方便以后直接调用 先简单放一些项目用到的 以后有时间再更新。

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) {

      }
    });
  }
 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值