这篇文章主要介绍了Android开发之Animations动画用法,结合实例形式详细分析了Animations动画的类型、组成、模式及对应的使用技巧,需要的朋友可以参考下
正文
Android开发之Animations动画用法实例详解
本文实例讲述了android开发之animations用法。分享给大家供大家参考,具体如下:
一、动画类型
android的animation由四种类型组成:alpha、scale、translate、rotate
xml配置文件中
java code代码中
二、android动画模式
animation主要有两种动画模式:tweened 和 frame
一种是tweened animation(渐变动画)
一种是frame by frame(画面转换动画)
三、xml文件中定义动画
① 打开eclipse,新建android工程
② 在res目录中新建anim文件夹
③ 在anim目录中新建一个myanim.xml(注意文件名小写)
④ 加入xml的动画代码
|
1
2
3
4
5
6
7
|
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha/> <scale/> <translate/> <rotate/></set> |
四、android xml动画解析
1. alpha
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" ><alphaandroid:fromalpha="0.1"android:toalpha="1.0"android:duration="3000"/><!-- 透明度控制动画效果 alpha 浮点型值: fromalpha 属性为动画起始时透明度 toalpha 属性为动画结束时透明度 说明: 0.0表示完全透明 1.0表示完全不透明 以上值取0.0-1.0之间的float数据类型的数字 长整型值: duration 属性为动画持续时间 说明: 时间以毫秒为单位--></set> |
2. scale
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:interpolator= "@android:anim/accelerate_decelerate_interpolator" android:fromxscale="0.0" android:toxscale="1.4" android:fromyscale="0.0" android:toyscale="1.4" android:pivotx="50%" android:pivoty="50%" android:fillafter="false" android:duration="700" /></set><!-- 尺寸伸缩动画效果 scale 属性:interpolator 指定一个动画的插入器 在我试验过程中,使用android.res.anim中的资源时候发现 有三种动画插入器: accelerate_decelerate_interpolator 加速-减速 动画插入器 accelerate_interpolator 加速-动画插入器 decelerate_interpolator 减速- 动画插入器 其他的属于特定的动画效果 浮点型值: fromxscale 属性为动画起始时 x坐标上的伸缩尺寸 toxscale 属性为动画结束时 x坐标上的伸缩尺寸 fromyscale 属性为动画起始时y坐标上的伸缩尺寸 toyscale 属性为动画结束时y坐标上的伸缩尺寸 说明: 以上四种属性值 0.0表示收缩到没有 1.0表示正常无伸缩 值小于1.0表示收缩 值大于1.0表示放大 pivotx 属性为动画相对于物件的x坐标的开始位置 pivoty 属性为动画相对于物件的y坐标的开始位置 说明: 以上两个属性值 从0%-100%中取值 50%为物件的x或y方向坐标上的中点位置 长整型值: duration 属性为动画持续时间 说明: 时间以毫秒为单位 布尔型值: fillafter 属性 当设置为true ,该动画转化在动画结束后被应用--> |
3. translate
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translateandroid:fromxdelta="30"android:toxdelta="-80"android:fromydelta="30"android:toydelta="300"android:duration="2000"/><!-- translate 位置转移动画效果 整型值: fromxdelta 属性为动画起始时 x坐标上的位置 toxdelta 属性为动画结束时 x坐标上的位置 fromydelta 属性为动画起始时 y坐标上的位置 toydelta 属性为动画结束时 y坐标上的位置 注意: 没有指定fromxtype toxtype fromytype toytype 时候, 默认是以自己为相对参照物 长整型值: duration 属性为动画持续时间 说明: 时间以毫秒为单位--></set> |
4. rotate
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromdegrees="0" android:todegrees="+350" android:pivotx="50%" android:pivoty="50%" android:duration="3000" /><!-- rotate 旋转动画效果 属性:interpolator 指定一个动画的插入器 在我试验过程中,使用android.res.anim中的资源时候发现 有三种动画插入器: accelerate_decelerate_interpolator 加速-减速 动画插入器 accelerate_interpolator 加速-动画插入器 decelerate_interpolator 减速- 动画插入器 其他的属于特定的动画效果 浮点数型值: fromdegrees 属性为动画起始时物件的角度 todegrees 属性为动画结束时物件旋转的角度 可以大于360度 说明: 当角度为负数——表示逆时针旋转 当角度为正数——表示顺时针旋转 (负数from——to正数:顺时针旋转) (负数from——to负数:逆时针旋转) (正数from——to正数:顺时针旋转) (正数from——to负数:逆时针旋转) pivotx 属性为动画相对于物件的x坐标的开始位置 pivoty 属性为动画相对于物件的y坐标的开始位置 说明: 以上两个属性值 从0%-100%中取值 50%为物件的x或y方向坐标上的中点位置 长整型值: duration 属性为动画持续时间 说明: 时间以毫秒为单位--></set> |
xml中使用动画效果
|
1
2
3
4
5
6
|
public static animation loadanimation (context context, int id)//第一个参数context为程序的上下文//第二个参数id为动画xml文件的引用//例子:myanimation= animationutils.loadanimation(this, r.anim.my_action);//使用animationutils类的静态方法loadanimation()来加载xml中的动画xml文件 |
五、java代码中定义动画
|
1
2
3
4
5
6
7
8
9
10
11
12
|
//在代码中定义 动画实例对象private animation myanimation_alpha;private animation myanimation_scale;private animation myanimation_translate;private animation myanimation_rotate;//根据各自的构造方法来初始化一个实例对象myanimation_alpha = new alphaanimation(0.1f, 1.0f);myanimation_scale = new scaleanimation(0.0f, 1.4f, 0.0f, 1.4f, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f);myanimation_translate = new translateanimation(30.0f, -80.0f, 30.0f, 300.0f);myanimation_rotate = new rotateanimation(0.0f, +350.0f, animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f); |
六、android 代码动画解析
1. alphaanimation
alphaanimation类对象定义
1. private alphaanimation myanimation_alpha;
alphaanimation类对象构造
|
1
2
3
4
5
6
7
|
alphaanimation(float fromalpha, float toalpha)//第一个参数fromalpha为 动画开始时候透明度//第二个参数toalpha为 动画结束时候透明度myanimation_alpha = new alphaanimation(0.1f, 1.0f);//说明:// 0.0表示完全透明// 1.0表示完全不透明 |
设置动画持续时间
|
1
2
|
myanimation_alpha.setduration(5000);//设置时间持续时间为 5000毫秒 |
2. scaleanimation
scaleanimation类对象定义
|
1
|
private scaleanimation myanimation_scale; |
scaleanimation类对象构造
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
scaleanimation(float fromx, float tox, float fromy, float toy, int pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue)//第一个参数fromx为动画起始时 x坐标上的伸缩尺寸//第二个参数tox为动画结束时 x坐标上的伸缩尺寸//第三个参数fromy为动画起始时y坐标上的伸缩尺寸//第四个参数toy为动画结束时y坐标上的伸缩尺寸/*说明: 以上四种属性值 0.0表示收缩到没有 1.0表示正常无伸缩 值小于1.0表示收缩 值大于1.0表示放大*///第五个参数pivotxtype为动画在x轴相对于物件位置类型//第六个参数pivotxvalue为动画相对于物件的x坐标的开始位置//第七个参数pivotxtype为动画在y轴相对于物件位置类型//第八个参数pivotyvalue为动画相对于物件的y坐标的开始位置myanimation_scale = new scaleanimation(0.0f, 1.4f, 0.0f, 1.4f, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f); |
设置动画持续时间
|
1
2
|
myanimation_scale.setduration(700);//设置时间持续时间为 700毫秒 |
3. translateanimation
ranslateanimation类对象定义
|
1
|
private translateanimation myanimation_translate; |
translateanimation类对象构造
|
1
2
3
4
5
6
|
translateanimation(float fromxdelta, float toxdelta, float fromydelta, float toydelta)//第一个参数fromxdelta为动画起始时 x坐标上的移动位置//第二个参数toxdelta为动画结束时 x坐标上的移动位置//第三个参数fromydelta为动画起始时y坐标上的移动位置//第四个参数toydelta为动画结束时y坐标上的移动位置 |
设置动画持续时间
|
1
2
3
|
myanimation_translate = new translateanimation(10f, 100f, 10f, 100f);myanimation_translate.setduration(2000);//设置时间持续时间为 2000毫秒 |
4. rotateanimation
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
rotateanimation类对象定义private rotateanimation myanimation_rotate;rotateanimation类对象构造rotateanimation(float fromdegrees, float todegrees, int pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue)//第一个参数fromdegrees为动画起始时的旋转角度//第二个参数todegrees为动画旋转到的角度//第三个参数pivotxtype为动画在x轴相对于物件位置类型//第四个参数pivotxvalue为动画相对于物件的x坐标的开始位置//第五个参数pivotxtype为动画在y轴相对于物件位置类型//第六个参数pivotyvalue为动画相对于物件的y坐标的开始位置myanimation_rotate = new rotateanimation(0.0f, +350.0f, animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f); |
设置动画持续时间
|
1
2
|
myanimation_rotate.setduration(3000);//设置时间持续时间为 3000毫秒 |
如何java代码中使用动画效果
使用从view父类继承过来的方法startanimation()来为view或是子类view等等添加一个动画效果
|
1
2
3
4
5
|
public void startanimation (animation animation)view.startanimation(myanimation_alpha);view.startanimation(myanimation_scale);view.startanimation(myanimation_translate);view.startanimation(myanimation_rotate); |
希望本文所述对大家android程序设计有所帮助。

发表评论