动画工具类

  1 public class AbAnimationUtil {
  2     
  3     /** 定义动画的时间. */
  4     public final static long aniDurationMillis = 1L;
  5 
  6     /**
  7      * 用来改变当前选中区域的放大动画效果
  8      * 从1.0f放大1.2f倍数
  9      *
 10      * @param view the view
 11      * @param scale the scale
 12      */
 13     public static void largerView(View view, float scale) {
 14         if (view == null)
 15             return;
 16 
 17         // 置于所有view最上层
 18         view.bringToFront();
 19         int width = view.getWidth();
 20         float animationSize = 1 + scale / width;
 21         scaleView(view, animationSize);
 22     }
 23 
 24     /**
 25      * 用来还原当前选中区域的还原动画效果.
 26      *
 27      * @param view the view
 28      * @param scale the scale
 29      */
 30     public static void restoreLargerView(View view, float scale) {
 31         if (view == null)
 32             return;
 33         int width = view.getWidth();
 34         float toSize = 1 + scale / width;
 35         // 从1.2f缩小1.0f倍数
 36         scaleView(view, -1 * toSize);
 37     }
 38 
 39     /**
 40      * 缩放View的显示.
 41      *
 42      * @param view 需要改变的View
 43      * @param toSize 缩放的大小,其中正值代表放大,负值代表缩小,数值代表缩放的倍数
 44      */
 45     private static void scaleView(final View view, float toSize) {
 46         ScaleAnimation scale = null;
 47         if (toSize == 0) {
 48             return;
 49         } else if (toSize > 0) {
 50             scale = new ScaleAnimation(1.0f, toSize, 1.0f, toSize,
 51                     Animation.RELATIVE_TO_SELF, 0.5f,
 52                     Animation.RELATIVE_TO_SELF, 0.5f);
 53         } else {
 54             scale = new ScaleAnimation(toSize * (-1), 1.0f, toSize * (-1),
 55                     1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
 56                     Animation.RELATIVE_TO_SELF, 0.5f);
 57         }
 58         scale.setDuration(aniDurationMillis);
 59         scale.setInterpolator(new AccelerateDecelerateInterpolator());
 60         scale.setFillAfter(true);
 61         view.startAnimation(scale);
 62     }
 63     
 64     /**
 65      * 跳动-跳起动画.
 66      *
 67      * @param view the view
 68      * @param offsetY the offset y
 69      */
 70     private void playJumpAnimation(final View view,final float offsetY) {
 71         float originalY = 0;
 72         float finalY = - offsetY;
 73         AnimationSet animationSet = new AnimationSet(true);
 74         animationSet.addAnimation(new TranslateAnimation(0, 0, originalY,finalY));
 75         animationSet.setDuration(300);
 76         animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
 77         animationSet.setFillAfter(true);
 78 
 79         animationSet.setAnimationListener(new AnimationListener() {
 80 
 81             @Override
 82             public void onAnimationStart(Animation animation) {
 83             }
 84 
 85             @Override
 86             public void onAnimationRepeat(Animation animation) {
 87             }
 88 
 89             @Override
 90             public void onAnimationEnd(Animation animation) {
 91                 playLandAnimation(view,offsetY);
 92             }
 93         });
 94 
 95         view.startAnimation(animationSet);
 96     }
 97 
 98     /**
 99      * 跳动-落下动画.
100      *
101      * @param view the view
102      * @param offsetY the offset y
103      */
104     private void playLandAnimation(final View view,final float offsetY) {
105         float originalY =  - offsetY;
106         float finalY = 0;
107         AnimationSet animationSet = new AnimationSet(true);
108         animationSet.addAnimation(new TranslateAnimation(0, 0, originalY,finalY));
109         animationSet.setDuration(200);
110         animationSet.setInterpolator(new AccelerateInterpolator());
111         animationSet.setFillAfter(true);
112 
113         animationSet.setAnimationListener(new AnimationListener() {
114 
115             @Override
116             public void onAnimationStart(Animation animation) {
117             }
118 
119             @Override
120             public void onAnimationRepeat(Animation animation) {
121             }
122 
123             @Override
124             public void onAnimationEnd(Animation animation) {
125                 //两秒后再调
126                 view.postDelayed(new Runnable(){
127                     
128                     @Override
129                     public void run(){
130                         playJumpAnimation(view,offsetY);
131                     }
132                 }, 2000);
133             }
134         });
135 
136         view.startAnimation(animationSet);
137     }
138     
139     /**
140      * 旋转动画
141      * @param v
142      * @param durationMillis
143      * @param repeatCount  Animation.INFINITE
144      * @param repeatMode  Animation.RESTART
145      */
146     public static void playRotateAnimation(View v,long durationMillis,int repeatCount,int repeatMode) {
147         
148         //创建AnimationSet对象
149         AnimationSet animationSet = new AnimationSet(true);
150         //创建RotateAnimation对象
151         RotateAnimation rotateAnimation = new RotateAnimation(0f,+360f, 
152                     Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
153         //设置动画持续
154         rotateAnimation.setDuration(durationMillis);
155         rotateAnimation.setRepeatCount(repeatCount);
156         //Animation.RESTART
157         rotateAnimation.setRepeatMode(repeatMode);
158         //动画插入器
159         rotateAnimation.setInterpolator(v.getContext(), android.R.anim.decelerate_interpolator);
160         //添加到AnimationSet
161         animationSet.addAnimation(rotateAnimation);
162         
163         //开始动画 
164         v.startAnimation(animationSet);
165     }
166 
167 }
原文地址:https://www.cnblogs.com/eachann/p/4148273.html