1
+ 1 、如何在XML文件中定义动
2
+ (1 )打开AS ,新建Android工程
3
+ (2 )在res目录中新建anim文件夹
4
+ (3 )在anim目录中新建一个myanim .xml (注意文件名小写 )
5
+ (4 )加入XML的动画代码
6
+ <?xml version ="1.0" encoding ="utf-8" ?>
7
+ <set xmlns :android ="http://schemas.android.com/apk/res/android" >
8
+ <alpha />
9
+ <scale />
10
+ <translate />
11
+ <rotate />
12
+ </set >
13
+
14
+ /****渐变透明度动画效果****/
15
+ <?xml version ="1.0" encoding ="utf-8" ?>
16
+ <set xmlns :android ="http://schemas.android.com/apk/res/android" >
17
+ <alpha
18
+ android :fromAlpha ="0.1"
19
+ android :toAlpha ="1.0"
20
+ android :duration ="3000" />
21
+ <!-- 透明度控制动画效果 alpha
22
+ 浮点型值 :
23
+ fromAlpha 属性为动画起始时透明度
24
+ toAlpha 属性为动画结束时透明度
25
+ 说明 :
26
+ 0.0 表示完全透明
27
+ 1.0 表示完全不透明
28
+ 以上值取0 .0 -1.0 之间的float数据类型的数字
29
+
30
+ 长整型值 :
31
+ duration 属性为动画持续时间
32
+ 说明 :
33
+ 时间以毫秒为单位
34
+ -->
35
+ </set >
36
+
37
+ /**** 渐变尺寸伸缩动画效果****/
38
+ <?xml version ="1.0" encoding ="utf-8" ?>
39
+ <set xmlns :android ="http://schemas.android.com/apk/res/android" >
40
+ <scale
41
+ android :interpolator ="@android:anim/accelerate_decelerate_interpolator"
42
+ android :fromXScale ="0.0"
43
+ android :toXScale ="1.4"
44
+ android :fromYScale ="0.0"
45
+ android :toYScale ="1.4"
46
+ android :pivotX ="50%"
47
+ android :pivotY ="50%"
48
+ android :fillAfter ="false"
49
+ android :duration ="700" />
50
+ </set >
51
+ <!-- 尺寸伸缩动画效果 scale
52
+ 属性 :interpolator 指定一个动画的插入器
53
+ 在我试验过程中 ,使用android .res .anim中的资源时候发现
54
+ 有三种动画插入器 :
55
+ accelerate_decelerate_interpolator 加速 -减速 动画插入器
56
+ accelerate_interpolator 加速 -动画插入器
57
+ decelerate_interpolator 减速 - 动画插入器
58
+ 其他的属于特定的动画效果
59
+ 浮点型值 :
60
+
61
+ fromXScale 属性为动画起始时 X坐标上的伸缩尺寸
62
+ toXScale 属性为动画结束时 X坐标上的伸缩尺寸
63
+
64
+ fromYScale 属性为动画起始时Y坐标上的伸缩尺寸
65
+ toYScale 属性为动画结束时Y坐标上的伸缩尺寸
66
+
67
+ 说明 :
68
+ 以上四种属性值
69
+
70
+ 0.0 表示收缩到没有
71
+ 1.0 表示正常无伸缩
72
+ 值小于1 .0 表示收缩
73
+ 值大于1 .0 表示放大
74
+
75
+ pivotX 属性为动画相对于物件的X坐标的开始位置
76
+ pivotY 属性为动画相对于物件的Y坐标的开始位置
77
+
78
+ 说明 :
79
+ 以上两个属性值 从0 %-100 %中取值
80
+ 50 %为物件的X或Y方向坐标上的中点位置
81
+
82
+ 长整型值 :
83
+ duration 属性为动画持续时间
84
+ 说明 : 时间以毫秒为单位
85
+
86
+ 布尔型值 :
87
+ fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用
88
+ -->
89
+
90
+ /****画面转换位置移动动画效果****/
91
+ <?xml version ="1.0" encoding ="utf-8" ?>
92
+ <set xmlns :android ="http://schemas.android.com/apk/res/android" >
93
+ <translate
94
+ android :fromXDelta ="30"
95
+ android :toXDelta ="-80"
96
+ android :fromYDelta ="30"
97
+ android :toYDelta ="300"
98
+ android :duration ="2000"
99
+ />
100
+ <!-- translate 位置转移动画效果
101
+ 整型值 :
102
+ fromXDelta 属性为动画起始时 X坐标上的位置
103
+ toXDelta 属性为动画结束时 X坐标上的位置
104
+ fromYDelta 属性为动画起始时 Y坐标上的位置
105
+ toYDelta 属性为动画结束时 Y坐标上的位置
106
+ 注意 :
107
+ 没有指定fromXType toXType fromYType toYType 时候 ,
108
+ 默认是以自己为相对参照物
109
+ 长整型值 :
110
+ duration 属性为动画持续时间
111
+ 说明 : 时间以毫秒为单位
112
+ -->
113
+ </set >
114
+
115
+ /****画面转移旋转动画效果****/
116
+ <?xml version ="1.0" encoding ="utf-8" ?>
117
+ <set xmlns :android ="http://schemas.android.com/apk/res/android" >
118
+ <rotate
119
+ android :interpolator ="@android:anim/accelerate_decelerate_interpolator"
120
+ android :fromDegrees ="0" //属性为动画起始时物件的角度
121
+ android :toDegrees ="+350" //属性为动画结束时物件旋转的角度(可以大于360度)
122
+ android :pivotX ="50%"
123
+ android :pivotY ="50%"
124
+ android :duration ="3000" />
125
+ <!-- rotate 旋转动画效果
126
+ 属性 :interpolator 指定一个动画的插入器
127
+ 在我试验过程中 ,使用android .res .anim中的资源时候发现
128
+ 有三种动画插入器 :
129
+ accelerate_decelerate_interpolator 加速 -减速 动画插入器
130
+ accelerate_interpolator 加速 -动画插入器
131
+ decelerate_interpolator 减速 - 动画插入器
132
+ 其他的属于特定的动画效果
133
+
134
+ 浮点数型值 :
135
+ fromDegrees 属性为动画起始时物件的角度
136
+ toDegrees 属性为动画结束时物件旋转的角度 可以大于360度
137
+
138
+
139
+ 说明 :
140
+ 当角度为负数 ——表示逆时针旋转
141
+ 当角度为正数 ——表示顺时针旋转
142
+ (负数from ——to正数 :顺时针旋转 )
143
+ (负数from ——to负数 :逆时针旋转 )
144
+ (正数from ——to正数 :顺时针旋转 )
145
+ (正数from ——to负数 :逆时针旋转 )
146
+
147
+ pivotX 属性为动画相对于物件的X坐标的开始位置
148
+ pivotY 属性为动画相对于物件的Y坐标的开始位置
149
+
150
+ 说明 : 以上两个属性值 从0 %-100 %中取值
151
+ 50 %为物件的X或Y方向坐标上的中点位置
152
+
153
+ 长整型值 :
154
+ duration 属性为动画持续时间
155
+ 说明 : 时间以毫秒为单位
156
+ -->
157
+ </set >
158
+ 2 、View Animation 动画 (补间动画 ):
159
+ Java Code 如下 :
160
+ private Animation myAnimation ;
161
+ @ Override
162
+ public void onClick (View v ) {
163
+ switch (v .getId ()) {
164
+ case R .id .btnAlpha :
165
+ /**
166
+ * 使用XML中的动画效果 第一个参数Context为程序的上下文 第二个参数id为动画XML文件的引用
167
+ */
168
+ myAnimation = AnimationUtils .loadAnimation (this , R .anim .alpha_anim );
169
+ imgPic .startAnimation (myAnimation );
170
+ break ;
171
+
172
+ case R .id .btnScale :
173
+ myAnimation = AnimationUtils .loadAnimation (this , R .anim .scale_anim );
174
+ imgPic .startAnimation (myAnimation );
175
+ break ;
176
+
177
+ case R .id .btnTranslate :
178
+ myAnimation = AnimationUtils .loadAnimation (this ,
179
+ R .anim .translate_anim );
180
+ imgPic .startAnimation (myAnimation );
181
+ break ;
182
+
183
+ case R .id .btnRotate :
184
+ myAnimation = AnimationUtils
185
+ .loadAnimation (this , R .anim .rotate_anim );
186
+ imgPic .startAnimation (myAnimation );
187
+ break ;
188
+
189
+ }
190
+ }
191
+
192
+ 3 、Drawable Animation (逐帧动画 ):
193
+ 步骤如下 :
194
+ (1 )在res /drawable目录添加动画系列图片素材 。
195
+ (2 )在drawable文件夹中添加动画Animation -list帧布局文件 。
196
+ <?xml version ="1.0" encoding ="utf-8" ?>
197
+ <!--
198
+ 根标签为animation -list ,其中oneshot代表着是否只展示一遍 ,设置为false会不停的循环播放动画
199
+ 根标签下 ,通过item标签对动画中的每一个图片进行声明
200
+ android :duration 表示展示所用的该图片的时间长度
201
+ -->
202
+ <animation -list xmlns :android ="http://schemas.android.com/apk/res/android"
203
+ android :oneshot ="false" >
204
+ <item
205
+ android :drawable ="@drawable/cmmusic_progress_1"
206
+ android :duration ="150" >
207
+ </item >
208
+ <item
209
+ android :drawable ="@drawable/cmmusic_progress_2"
210
+ android :duration ="150" >
211
+ </item >
212
+ <item
213
+ android :drawable ="@drawable/cmmusic_progress_3"
214
+ android :duration ="150" >
215
+ </item >
216
+ <item
217
+ android :drawable ="@drawable/cmmusic_progress_4"
218
+ android :duration ="150" >
219
+ </item >
220
+ <item
221
+ android :drawable ="@drawable/cmmusic_progress_5"
222
+ android :duration ="150" >
223
+ </item >
224
+ <item
225
+ android :drawable ="@drawable/cmmusic_progress_6"
226
+ android :duration ="150" >
227
+ </item >
228
+ <item
229
+ android :drawable ="@drawable/cmmusic_progress_7"
230
+ android :duration ="150" >
231
+ </item >
232
+ <item
233
+ android :drawable ="@drawable/cmmusic_progress_8"
234
+ android :duration ="150" >
235
+ </item >
236
+ </animation -list >
237
+ (3 )Java 代码如下 :
238
+ public class AnimationActivity extends Activity implements OnClickListener {
239
+ private ImageView imgPic ;
240
+ private Button btnStart , btnStop ;
241
+ private AnimationDrawable animationDrawable ;
242
+
243
+ @ Override
244
+ protected void onCreate (Bundle savedInstanceState ) {
245
+ super .onCreate (savedInstanceState );
246
+ setContentView (R .layout .activity_animation );
247
+
248
+ intiView ();
249
+
250
+ initData ();
251
+ }
252
+
253
+ /**
254
+ * 初始化组件
255
+ */
256
+ private void intiView () {
257
+ imgPic = (ImageView ) findViewById (R .id .imgPic );
258
+ btnStart = (Button ) findViewById (R .id .btnStart );
259
+ btnStop = (Button ) findViewById (R .id .btnStop );
260
+ }
261
+
262
+ /**
263
+ * 初始化数据
264
+ */
265
+ private void initData () {
266
+ btnStart .setOnClickListener (this );
267
+ btnStop .setOnClickListener (this );
268
+ //Sets a drawable as the content of this ImageView.
269
+ imgPic .setImageResource (R .drawable .loading_anim );
270
+ //给动画资源赋值
271
+ animationDrawable = (AnimationDrawable ) imgPic .getDrawable ();
272
+ }
273
+
274
+ @ Override
275
+ public void onClick (View v ) {
276
+ switch (v .getId ()) {
277
+ case R .id .btnStart :
278
+ animationDrawable .start ();//开始
279
+ break ;
280
+
281
+ case R .id .btnStop :
282
+ animationDrawable .stop (); //停止
283
+ break ;
284
+
285
+ }
286
+ }
287
+ }
288
+
289
+ 4 、Android 加载Gif动画的控件 :
290
+ https ://github.com/arvin505/gifloader
291
+
292
+ 动画详解参考网址入下 :http ://blog.csdn.net/yanbober/article/details/46481171
0 commit comments