动画相关的工具类AnimationUtil

动画相关的工具类

package com.flyou.utils;

import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

public class AnimationUtil {

  /** 定义动画的时间. */
  public final static long aniDurationMillis = 1L;

  /**
   * 用来改变当前选中区域的放大动画效果 从1.0f放大1.2f倍数
   * 
   * @param view
   *          the view
   * @param scale
   *          the scale
   */
  public static void largerView(View view, float scale) {
    if (view == null)
      return;

    // 置于所有view最上层
    view.bringToFront();
    int width = view.getWidth();
    float animationSize = 1 + scale / width;
    scaleView(view, animationSize);
  }

  /**
   * 用来还原当前选中区域的还原动画效果.
   * 
   * @param view
   *          the view
   * @param scale
   *          the scale
   */
  public static void restoreLargerView(View view, float scale) {
    if (view == null)
      return;
    int width = view.getWidth();
    float toSize = 1 + scale / width;
    // 从1.2f缩小1.0f倍数
    scaleView(view, -1 * toSize);
  }

  /**
   * 缩放View的显示.
   * 
   * @param view
   *          需要改变的View
   * @param toSize
   *          缩放的大小,其中正值代表放大,负值代表缩小,数值代表缩放的倍数
   */
  private static void scaleView(final View view, float toSize) {
    ScaleAnimation scale = null;
    if (toSize == 0) {
      return;
    } else if (toSize > 0) {
      scale = new ScaleAnimation(1.0f, toSize, 1.0f, toSize, Animation.RELATIVE_TO_SELF, 0.5f,
          Animation.RELATIVE_TO_SELF, 0.5f);
    } else {
      scale = new ScaleAnimation(toSize * (-1), 1.0f, toSize * (-1), 1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
          Animation.RELATIVE_TO_SELF, 0.5f);
    }
    scale.setDuration(aniDurationMillis);
    scale.setInterpolator(new AccelerateDecelerateInterpolator());
    scale.setFillAfter(true);
    view.startAnimation(scale);
  }

  /**
   * 跳动-跳起动画.
   * 
   * @param view
   *          the view
   * @param offsetY
   *          the offset y
   */
  private void playJumpAnimation(final View view, final float offsetY) {
    float originalY = 0;
    float finalY = -offsetY;
    AnimationSet animationSet = new AnimationSet(true);
    animationSet.addAnimation(new TranslateAnimation(0, 0, originalY, finalY));
    animationSet.setDuration(300);
    animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
    animationSet.setFillAfter(true);

    animationSet.setAnimationListener(new AnimationListener() {

      @Override
      public void onAnimationStart(Animation animation) {
      }

      @Override
      public void onAnimationRepeat(Animation animation) {
      }

      @Override
      public void onAnimationEnd(Animation animation) {
        playLandAnimation(view, offsetY);
      }
    });

    view.startAnimation(animationSet);
  }

  /**
   * 跳动-落下动画.
   * 
   * @param view
   *          the view
   * @param offsetY
   *          the offset y
   */
  private void playLandAnimation(final View view, final float offsetY) {
    float originalY = -offsetY;
    float finalY = 0;
    AnimationSet animationSet = new AnimationSet(true);
    animationSet.addAnimation(new TranslateAnimation(0, 0, originalY, finalY));
    animationSet.setDuration(200);
    animationSet.setInterpolator(new AccelerateInterpolator());
    animationSet.setFillAfter(true);

    animationSet.setAnimationListener(new AnimationListener() {

      @Override
      public void onAnimationStart(Animation animation) {
      }

      @Override
      public void onAnimationRepeat(Animation animation) {
      }

      @Override
      public void onAnimationEnd(Animation animation) {
        // 两秒后再调
        view.postDelayed(new Runnable() {

          @Override
          public void run() {
            playJumpAnimation(view, offsetY);
          }
        }, 2000);
      }
    });

    view.startAnimation(animationSet);
  }

  /**
   * 旋转动画
   * 
   * @param v
   * @param durationMillis
   * @param repeatCount
   *          Animation.INFINITE
   * @param repeatMode
   *          Animation.RESTART
   */
  public static void playRotateAnimation(View v, long durationMillis, int repeatCount, int repeatMode) {

    // 创建AnimationSet对象
    AnimationSet animationSet = new AnimationSet(true);
    // 创建RotateAnimation对象
    RotateAnimation rotateAnimation = new RotateAnimation(0f, +360f, Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
    // 设置动画持续
    rotateAnimation.setDuration(durationMillis);
    rotateAnimation.setRepeatCount(repeatCount);
    // Animation.RESTART
    rotateAnimation.setRepeatMode(repeatMode);
    // 动画插入器
    rotateAnimation.setInterpolator(v.getContext(), android.R.anim.decelerate_interpolator);
    // 添加到AnimationSet
    animationSet.addAnimation(rotateAnimation);

    // 开始动画
    v.startAnimation(animationSet);
  }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值