Android开发之补间动画、XML方式定义补间动画

四种补间动画:

  1、透明;

  2、缩放;

  3、位移;

  4、旋转;

 1 //点击按钮 实现iv 透明的效果  动画 
 2     public void click1(View v) { 
 3         //1.0意味着着完全不透明 0.0意味着完全透明
 4         AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);
 5         aa.setDuration(2000); //设置动画执行的时间
 6         aa.setRepeatCount(1); //设置重复的次数
 7         aa.setRepeatMode(Animation.REVERSE);//设置动画执行的模式
 8         //iv开始执行动画 
 9         iv.startAnimation(aa);
10         
11     }
12     
13 
14     //点击按钮 实现iv 执行一个旋转 动画 
15     public void click2(View v) { 
16         //fromDegrees 开始角度   toDegrees 结束角度
17 //        RotateAnimation  ra = new RotateAnimation(0, 360);    
18         RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
19         ra.setDuration(2000); //设置动画执行的时间
20         ra.setRepeatCount(1); //设置重复的次数
21         ra.setRepeatMode(Animation.REVERSE);//设置动画执行的模式
22         //iv开始执行动画 
23         iv.startAnimation(ra);
24         
25     }
26     
27     //点击按钮进行一个缩放动画
28     public void click3(View v) { 
29         ScaleAnimation sa = new ScaleAnimation(1.0f,2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
30         sa.setDuration(2000); //设置动画执行的时间
31         sa.setRepeatCount(1); //设置重复的次数
32         sa.setRepeatMode(Animation.REVERSE);//设置动画执行的模式
33         //iv开始执行动画 
34         iv.startAnimation(sa);
35     }
36 
37     //位移动画 
38     public void click4(View v){
39         TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.2f);
40          ta.setDuration(2000); //设置动画执行的时间
41          ta.setFillAfter(true);//当动画结束后 动画停留在结束位置
42          
43          //开始动画
44          iv.startAnimation(ta);
45     }
46     
47     //动画一起飞
48     public void click5(View v){
49         AnimationSet set = new AnimationSet(false);
50         
51         //透明动画
52         AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);
53         aa.setDuration(2000); //设置动画执行的时间
54         aa.setRepeatCount(1); //设置重复的次数
55         aa.setRepeatMode(Animation.REVERSE);//设置动画执行的模式
56         //旋转动画
57         RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
58         ra.setDuration(2000); //设置动画执行的时间
59         ra.setRepeatCount(1); //设置重复的次数
60         ra.setRepeatMode(Animation.REVERSE);//设置动画执行的模式
61         //缩放
62         ScaleAnimation sa = new ScaleAnimation(1.0f,2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
63         sa.setDuration(2000); //设置动画执行的时间
64         sa.setRepeatCount(1); //设置重复的次数
65         sa.setRepeatMode(Animation.REVERSE);//设置动画执行的模式
66         
67         TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.2f);
68          ta.setDuration(2000); //设置动画执行的时间
69          ta.setFillAfter(true);//当动画结束后 动画停留在结束位置
70          
71         //添加动画
72         set.addAnimation(aa);
73         set.addAnimation(ra);
74         set.addAnimation(sa);
75         set.addAnimation(ta);
76     
77         //最后一步 要记得 执行动画
78         iv.startAnimation(set);    
79     }

几个属性介绍:

 1、Duration:设置动画执行的时间;

 2、RepeatCount:动画的重复次数,如果要无限次播放,填写一个小于0的数,一般写-1;

 3、fillAfter:动画结束之后是否保持动画的最终状态;true,表示保持动画的最终状态

   4、fillBefore:动画结束之后是否保持动画开始前的状态;true,表示恢复到动画开始前的状态

   5、startOffset:动画的延迟时长,单位是毫秒

   6、RepeatMode:动画的执行模式:

  • reverse:动画是从一开始的1.0渐变成0.3,然后在从0.3渐变为1.0,重复往返
  • restart:冻哈是从一开始的1.0渐变成0.3,然后图片从0.3突变为1.0,然后在渐变成0.3,重复往返,

XML定义补间动画:

透明:

1 <alpha
2     xmlns:android="http://schemas.android.com/apk/res/android"
3     android:fromAlpha="1.0"
4     android:toAlpha="0.0"
5     android:duration="200"
6     android:repeatMode="reverse"
7     android:repeatCount="2">
8 </alpha>

旋转:

 1 <rotate
 2     android:fromDegrees="0"
 3     android:toDegrees="360"
 4     android:pivotX="50%"
 5     android:pivotY="50%"
 6     android:repeatCount="1"
 7     android:repeatMode="reverse"
 8     android:duration="2000"
 9     xmlns:android="http://schemas.android.com/apk/res/android">
10 </rotate>

缩放:

 1 <scale
 2     android:fromXScale="1.0"
 3     android:toXScale="2.0"
 4     android:fromYScale="1.0"
 5     android:toYScale="2.0"
 6     android:pivotX="50%"
 7     android:pivotY="50%"
 8     android:repeatMode="reverse"
 9     android:repeatCount="1"
10     android:duration="2000"
11     xmlns:android="http://schemas.android.com/apk/res/android">
12 </scale>

位移:

1 <translate
2     android:fromXDelta="0%p"
3     android:toXDelta="0%p"
4     android:fromYDelta="0%p"
5     android:toYDelta="20%p"
6     android:fillAfter="true"
7     android:duration="2000"
8     xmlns:android="http://schemas.android.com/apk/res/android">
9 </translate>

实现XML动画:

1 public void click1(View v) { 
2         Animation aa = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
3         //iv开始执行动画 
4         iv.startAnimation(aa);    
5     }
GitHub:https://github.com/godfunc
博客园:http://www.cnblogs.com/godfunc
Copyright ©2019 Godfunc
原文地址:https://www.cnblogs.com/Godfunc/p/6071663.html