本文实例讲述了android编程之animation动画用法。分享给大家供大家参考,具体如下:
animations
一、animations介绍
animations是一个实现android ui界面动画效果的api,animations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。
二、animations的分类
animations从总体上可以分为两大类:
1.tweened animations:该类animations提供了旋转、移动、伸展和淡出等效果。alpha——淡入淡出,scale——缩放效果,rotate——旋转,translate——移动效果。
2.frame-by-frame animations:这一类animations可以创建一个drawable序列,这些drawable可以按照指定的时间间歇一个一个的显示。
三、animations的使用方法(代码中使用)
animations extends object implements cloneable
使用tweenedanimations的步骤:
1.创建一个animationset对象(animation子类);
2.增加需要创建相应的animation对象;
3.更加项目的需求,为animation对象设置相应的数据;
4.将animatin对象添加到animationset对象当中;
5.使用控件对象开始执行animationset。
tweened animations的分类
1、alpha:淡入淡出效果
2、scale:缩放效果
3、rotate:旋转效果
4、translate:移动效果
animation的四个子类:
alphaanimation、translateanimation、scaleanimation、rotateanimation
四、具体实现
1、main.xml
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
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:orientation= "vertical" >
<linearlayout
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:orientation= "horizontal" >
<button
android:id= "@+id/rotatebutton"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "旋转" />
<button
android:id= "@+id/scalebutton"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "缩放" />
<button
android:id= "@+id/alphabutton"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "淡入淡出" />
<button
android:id= "@+id/translatebutton"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "移动" />
</linearlayout>
<linearlayout
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:orientation= "vertical" >
<imageview
android:id= "@+id/image"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_centerinparent= "true"
android:src= "@drawable/an" />
</linearlayout>
</linearlayout>
|
2、.java文件
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
importandroid.app.activity;
importandroid.os.bundle;
importandroid.view.view;
importandroid.view.view.onclicklistener;
import android.view.animation.alphaanimation;
import android.view.animation.animation;
importandroid.view.animation.animationset;
importandroid.view.animation.rotateanimation;
importandroid.view.animation.scaleanimation;
import android.view.animation.translateanimation;
importandroid.widget.button;
importandroid.widget.imageview;
public class animation1activity extends activity {
private button rotatebutton = null ;
private button scalebutton = null ;
private button alphabutton = null ;
private button translatebutton = null ;
private imageview image = null ;
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
rotatebutton = (button)findviewbyid(r.id.rotatebutton);
scalebutton = (button)findviewbyid(r.id.scalebutton);
alphabutton = (button)findviewbyid(r.id.alphabutton);
translatebutton = (button)findviewbyid(r.id.translatebutton);
image = (imageview)findviewbyid(r.id.image);
rotatebutton.setonclicklistener(newrotatebuttonlistener());
scalebutton.setonclicklistener(newscalebuttonlistener());
alphabutton.setonclicklistener(newalphabuttonlistener());
translatebutton.setonclicklistener(
new translatebuttonlistener());
}
class alphabuttonlistener implementsonclicklistener{
public void onclick(view v) {
//创建一个animationset对象,参数为boolean型,
//true表示使用animation的interpolator,false则是使用自己的
animationset animationset = new animationset( true );
//创建一个alphaanimation对象,参数从完全的透明度,到完全的不透明
alphaanimation alphaanimation = new alphaanimation( 1 , 0 );
//设置动画执行的时间
alphaanimation.setduration( 500 );
//将alphaanimation对象添加到animationset当中
animationset.addanimation(alphaanimation);
//使用imageview的startanimation方法执行动画
image.startanimation(animationset);
}
}
class rotatebuttonlistener implementsonclicklistener{
public void onclick(view v) {
animationset animationset = new animationset( true );
//参数1:从哪个旋转角度开始
//参数2:转到什么角度
//后4个参数用于设置围绕着旋转的圆的圆心在哪里
//参数3:确定x轴坐标的类型,有absolut绝对坐标、relative_to_self相对于自身坐标、relative_to_parent相对于父控件的坐标
//参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数5:确定y轴坐标的类型
//参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
rotateanimation rotateanimation = new rotateanimation( 0 , 360 ,
animation.relative_to_self, 0 .5f,
animation.relative_to_self, 0 .5f);
rotateanimation.setduration( 1000 );
animationset.addanimation(rotateanimation);
image.startanimation(animationset);
}
}
class scalebuttonlistener implementsonclicklistener{
public void onclick(view v) {
animationset animationset = new animationset( true );
//参数1:x轴的初始值
//参数2:x轴收缩后的值
//参数3:y轴的初始值
//参数4:y轴收缩后的值
//参数5:确定x轴坐标的类型
//参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数7:确定y轴坐标的类型
//参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
scaleanimation scaleanimation = new scaleanimation(
0 , 0 .1f, 0 , 0 .1f,
animation.relative_to_self, 0 .5f,
animation.relative_to_self, 0 .5f);
scaleanimation.setduration( 1000 );
animationset.addanimation(scaleanimation);
image.startanimation(animationset);
}
}
class translatebuttonlistener implementsonclicklistener{
public void onclick(view v) {
animationset animationset = new animationset( true );
//参数1~2:x轴的开始位置
//参数3~4:y轴的开始位置
//参数5~6:x轴的结束位置
//参数7~8:x轴的结束位置
translateanimation translateanimation =
new translateanimation(
animation.relative_to_self,0f,
animation.relative_to_self, 0 .5f,
animation.relative_to_self,0f,
animation.relative_to_self, 0 .5f);
translateanimation.setduration( 1000 );
animationset.addanimation(translateanimation);
image.startanimation(animationset);
}
}
}
|
tween animations的通用方法
1、setduration(long durationmills)
设置动画持续时间(单位:毫秒)
2、setfillafter(boolean fillafter)
如果fillafter的值为true,则动画执行后,控件将停留在执行结束的状态
3、setfillbefore(boolean fillbefore)
如果fillbefore的值为true,则动画执行后,控件将回到动画执行之前的状态
4、setstartoffset(long startoffset)
设置动画执行之前的等待时间
5、setrepeatcount(int repeatcount)
设置动画重复执行的次数
在代码中使用animations可以很方便的调试、运行,但是代码的可重用性差,重复代码多。同样可以在xml文件中配置animations,这样做可维护性变高了,只不过不容易进行调试。
一、在xml中使用animations步骤
1.在res文件夹下建立一个anim文件夹;
2.创建xml文件,并首先加入set标签,更改标签如下:
1
2
3
4
|
<?xml version= "1.0" encoding= "utf-8" ?>
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/accelerate_interpolator" >
</set>
|
3.在该标签当中加入rotate,alpha,scale或者translate标签;
1
2
3
4
5
|
<alpha
android:fromalpha= "1.0"
android:toalpha= "0.0"
android:startoffset= "500"
android:duration= "500" />
|
4.在代码当中使用animationutils当中装载xml文件,并生成animation对象。因为animation是animationset的子类,所以向上转型,用animation对象接收。
1
2
3
4
|
animation animation = animationutils.loadanimation(
animation1activity. this , r.anim.alpha);
// 启动动画
image.startanimation(animation);
|
二、具体实现
1、 alpha.xml
1
2
3
4
5
6
7
8
9
10
|
<?xml version= "1.0" encoding= "utf-8" ?>
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/accelerate_interpolator" >
<!-- fromalpha和toalpha是起始透明度和结束时透明度 -->
<alpha
android:fromalpha= "1.0"
android:toalpha= "0.0"
android:startoffset= "500"
android:duration= "500" />
</set>
|
2、 rotate.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version= "1.0" encoding= "utf-8" ?>
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/accelerate_interpolator" >
<!--
fromdegrees:开始的角度
todegrees:结束的角度,+表示是正的
pivotx:用于设置旋转时的x轴坐标
例
1 )当值为 "50" ,表示使用绝对位置定位
2 )当值为 "50%" ,表示使用相对于控件本身定位
3 )当值为 "50%p" ,表示使用相对于控件的父控件定位
pivoty:用于设置旋转时的y轴坐标
-->
<rotate
android:fromdegrees= "0"
android:todegrees= "+360"
android:pivotx= "50%"
android:pivoty= "50%"
android:duration= "1000" />
</set>
|
3、 scale.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version= "1.0" encoding= "utf-8" ?>
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/accelerate_interpolator" >
<!--
起始x轴坐标
止x轴坐标
始y轴坐标
止y轴坐标
轴的坐标
轴的坐标
-->
<scale
android:fromxscale= "1.0"
android:toxscale= "0.0"
android:fromyscale= "1.0"
android:toyscale= "0.0"
android:pivotx= "50%"
android:pivoty= "50%"
android:duration= "1000" />
</set>
|
4、 translate.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?xml version= "1.0" encoding= "utf-8" ?>
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/accelerate_interpolator" >
<!--
始x轴坐标
止x轴坐标
始y轴坐标
止y轴坐标
-->
<translate
android:fromxdelta= "0%"
android:toxdelta= "100%"
android:fromydelta= "0%"
android:toydelta= "100%"
android:duration= "2000" />
</set>
|
5、 .java文件
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
importandroid.app.activity;
importandroid.os.bundle;
importandroid.view.view;
importandroid.view.view.onclicklistener;
import android.view.animation.animation;
importandroid.view.animation.animationutils;
import android.widget.button;
import android.widget.imageview;
public class animation1activity extends activity {
private button rotatebutton = null ;
private button scalebutton = null ;
private button alphabutton = null ;
private button translatebutton = null ;
private imageview image = null ;
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
rotatebutton = (button) findviewbyid(r.id.rotatebutton);
scalebutton = (button) findviewbyid(r.id.scalebutton);
alphabutton = (button) findviewbyid(r.id.alphabutton);
translatebutton = (button) findviewbyid(r.id.translatebutton);
image = (imageview) findviewbyid(r.id.image);
rotatebutton.setonclicklistener(newrotatebuttonlistener());
scalebutton.setonclicklistener(newscalebuttonlistener());
alphabutton.setonclicklistener(newalphabuttonlistener());
translatebutton.setonclicklistener(newtranslatebuttonlistener());
}
class alphabuttonlistener implementsonclicklistener {
public void onclick(view v) {
// 使用animationutils装载动画配置文件
animation animation = animationutils.loadanimation(
animation1activity. this , r.anim.alpha);
// 启动动画
image.startanimation(animation);
}
}
class rotatebuttonlistener implementsonclicklistener {
public void onclick(view v) {
animation animation = animationutils.loadanimation(
animation1activity. this , r.anim.rotate);
image.startanimation(animation);
}
}
class scalebuttonlistener implementsonclicklistener {
public void onclick(view v) {
animation animation = animationutils.loadanimation(
animation1activity. this , r.anim.scale);
image.startanimation(animation);
}
}
class translatebuttonlistener implementsonclicklistener {
public void onclick(view v) {
animation animation = animationutils.loadanimation(animation1activity. this , r.anim.translate);
image.startanimation(animation);
}
}
}
|
animationset的具体使用方法
1.animationset是animation的子类;
2.一个animationset包含了一系列的animation;
3.针对animationset设置一些animation的常见属性(如startoffset,duration等),可以被包含在animationset当中的animation集成;
例:一个animationset中有两个animation,效果叠加
第一种方法:
doubleani.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?xml version= "1.0" encoding= "utf-8" ?>
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/accelerate_interpolator"
android:shareinterpolator= "true" >
<!-- fromalpha和toalpha是起始透明度和结束时透明度 -->
<alpha
android:fromalpha= "1.0"
android:toalpha= "0.0"
android:startoffset= "500"
android:duration= "500" />
<translate
android:fromxdelta= "0%"
android:toxdelta= "100%"
android:fromydelta= "0%"
android:toydelta= "100%"
android:duration= "2000" />
</set>
|
.java文件中
1
2
3
4
5
6
7
8
9
|
classdoublebuttonlistener implements onclicklistener {
public void onclick(view v) {
// 使用animationutils装载动画配置文件
animation animation = animationutils.loadanimation(
animation2activity. this , r.anim. doubleani);
// 启动动画
image.startanimation(animation);
}
}
|
第二种方法:
.java文件中
1
2
3
4
5
6
7
8
9
10
11
12
13
|
classdoublebuttonlistener implements onclicklistener {
public void onclick(view v) {
animationset animationset = new animationset( true );
alphaanimation alphaanimation = new alphaanimation( 1 , 0 );
rotateanimation rotateanimation = new rotateanimation( 0 , 360 ,
animation.relative_to_self, 0 .5f,
animation.relative_to_self, 0 .5f);
rotateanimation.setduration( 1000 );
animationset.addanimation(rotateanimation);
animationset.addanimation(alphaanimation);
image.startanimation(animationset);
}
}
|
interpolator的具体使用方法
interpolator定义了动画变化的速率,在animations框架当中定义了以下几种interpolator
? acceleratedecelerateinterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候速率快。
? accelerateinterpolator:在动画开始的地方速率改变比较慢,然后开始加速
? cycleinterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
? decelerateinterpolator:在动画开始的地方速率改变比较慢,然后开始减速
? linearinterpolator:动画以均匀的速率改变
分为以下几种情况:
1、在set标签中
1
2
|
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/accelerate_interpolator" />
|
2、如果在一个set标签中包含多个动画效果,如果想让这些动画效果共享一个interpolator。
3、如果不想共享一个interpolator,则设置android:shareinterpolator="true",并且需要在每一个动画效果处添加interpolator。
1
2
3
4
5
6
|
<alpha
android:interpolator= "@android:anim/accelerate_decelerate_interpolator"
android:fromalpha= "1.0"
android:toalpha= "0.0"
android:startoffset= "500"
android:duration= "500" />
|
4、如果是在代码上设置共享一个interpolator,则可以在animationset设置interpolator。
1
2
|
animationset animationset = newanimationset( true );
animationset.setinterpolator( new accelerateinterpolator());
|
5、如果不设置共享一个interpolator则可以在每一个animation对象上面设置interpolator。
1
2
3
|
animationset animationset = newanimationset( false );
alphaanimation.setinterpolator( new accelerateinterpolator());
rotateanimation.setinterpolator( new decelerateinterpolator());
|
frame-by-frame animations的使用方法
frame-by-frame animations是一帧一帧的格式显示动画效果。类似于电影胶片拍摄的手法。
main.xml
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
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
<linearlayout
android:orientation= "horizontal"
android:layout_height= "wrap_content"
android:layout_width= "wrap_content" >
<button
android:id= "@+id/button"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "运动" />
</linearlayout>
<linearlayout
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
<imageview
android:id= "@+id/image"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_centerinparent= "true" />
</linearlayout>
</linearlayout>
|
3、anim.xml
1
2
3
4
5
6
7
8
9
10
|
<?xml version= "1.0" encoding= "utf-8" ?>
<animation-list xmlns:android= "http://schemas.android.com/apk/res/android"
android:oneshot= "false" >
<item android:drawable= "@drawable/a_01" android:duration= "50" />
<item android:drawable= "@drawable/a_02" android:duration= "50" />
<item android:drawable= "@drawable/a_03" android:duration= "50" />
<item android:drawable= "@drawable/a_04" android:duration= "50" />
<item android:drawable= "@drawable/a_05" android:duration= "50" />
<item android:drawable= "@drawable/a_06" android:duration= "50" />
</animation-list>
|
4、.java文件
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
|
importandroid.app.activity;
importandroid.graphics.drawable.animationdrawable;
importandroid.os.bundle;
importandroid.view.view;
importandroid.view.view.onclicklistener;
importandroid.widget.button;
importandroid.widget.imageview;
public class animationsactivity extends activity {
private button button = null ;
private imageview imageview = null ;
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
button = (button)findviewbyid(r.id.button);
imageview = (imageview)findviewbyid(r.id.image);
button.setonclicklistener(newbuttonlistener());
}
class buttonlistener implementsonclicklistener{
public void onclick(view v) {
imageview.setbackgroundresource(r.anim.anim);
animationdrawable animationdrawable = (animationdrawable)
imageview.getbackground();
animationdrawable.start();
}
}
}
|
layoutanimationscontroller
1、什么是layoutanimationscontroller
layoutanimationscontroller可以用于实现使多个控件按顺序一个一个的显示。
1)layoutanimationscontroller用于为一个layout里面的控件,或者是一个viewgroup里面的控件设置统一的动画效果。
2)每一个控件都有相同的动画效果。
3)控件的动画效果可以在不同的时间显示出来。
4)layoutanimationscontroller可以在xml文件当中设置,以可以在代码当中进行设置。
2、在xml当中使用layoutanimationcontroller
1)在res/anim文件夹下创建一个名为list_anim_layout.xml文件:
android:delay - 动画间隔时间;子类动画时间间隔 (延迟) 70% 也可以是一个浮点数 如“1.2”等
android:animationorder - 动画执行的循序(normal:顺序,random:随机,reverse:反向显示)
android:animation – 引用动画效果文件
1
2
3
4
5
|
<layoutanimation
xmlns:android= "http://schemas.android.com/apk/res/android"
android:delay= "0.5"
android:animationorder= "normal"
android:animation= "@anim/list_anim" />
|
2)创建list_anim.xml文件,设置动画效果
1
2
3
4
5
6
7
8
9
|
<?xml version= "1.0" encoding= "utf-8" ?>
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/accelerate_interpolator"
android:shareinterpolator= "true" >
<alpha
android:fromalpha= "0.0"
android:toalpha= "1.0"
android:duration= "1000" />
</set>
|
3)在布局文件main.xml当中为listview添加如下配置
1
2
3
4
5
6
|
<listview
android:id= "@id/android:list"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:scrollbars= "vertical"
android:layoutanimation= "@anim/list_anim_layout" />
|
4)程序结构
5)list_anim_layout.xml
1
2
3
4
5
|
<layoutanimation
xmlns:android= "http://schemas.android.com/apk/res/android"
android:delay= "0.5"
android:animationorder= "normal"
android:animation= "@anim/list_anim" />
|
6)list_anim.xml
1
2
3
4
5
6
7
8
9
|
<?xml version= "1.0" encoding= "utf-8" ?>
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/accelerate_interpolator"
android:shareinterpolator= "true" >
<alpha
android:fromalpha= "0.0"
android:toalpha= "1.0"
android:duration= "1000" />
</set>
|
7)main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
>
<listview
android:id= "@id/android:list"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:scrollbars= "vertical"
android:layoutanimation= "@anim/list_anim_layout" />
<button
android:id= "@+id/button"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:text= "测试" />
</linearlayout>
|
8)item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:orientation= "horizontal"
android:paddingleft= "10dip"
android:paddingright= "10dip"
android:paddingtop= "1dip"
android:paddingbottom= "1dip" >
<textview android:id= "@+id/name"
android:layout_width= "180dip"
android:layout_height= "30dip"
android:textsize= "5pt"
android:singleline= "true" />
<textview android:id= "@+id/sex"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:textsize= "5pt"
android:singleline= "true" />
</linearlayout>
|
9)java文件
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
|
public class animation2activity extendslistactivity {
private button button = null ;
private listview listview = null ;
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
listview = getlistview();
button = (button)findviewbyid(r.id.button);
button.setonclicklistener(newbuttonlistener());
}
private listadapter createlistadapter() {
list<hashmap<string,string>> list =
new arraylist<hashmap<string,string>>();
hashmap<string,string> m1 = new hashmap<string,string>();
m1.put( "name" , "bauble" );
m1.put( "sex" , "male" );
hashmap<string,string> m2 = new hashmap<string,string>();
m2.put( "name" , "allorry" );
m2.put( "sex" , "male" );
hashmap<string,string> m3 = new hashmap<string,string>();
m3.put( "name" , "allotory" );
m3.put( "sex" , "male" );
hashmap<string,string> m4 = new hashmap<string,string>();
m4.put( "name" , "boolbe" );
m4.put( "sex" , "male" );
list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
simpleadapter simpleadapter = new simpleadapter(
this ,list,r.layout.item, new string[]{ "name" , "sex" },
new int []{r.id.name,r.id.sex});
return simpleadapter;
}
private class buttonlistener implementsonclicklistener{
public void onclick(view v) {
listview.setadapter(createlistadapter());
}
}
}
|
备注:要将整个动画效果设置到linerlayout中,可以这样设置:
1
2
3
4
5
6
|
<linearlayoutxmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:layoutanimation= "@anim/list_anim_layout"
>
|
3、在代码当中使用layoutanimationcontroller
1)去掉main.xml中的android:layoutanimation="@anim/list_anim_layout"/>
2)创建一个animation对象:可以通过装载xml文件,或者是直接使用animation的构造方法创建animation对象;
1
2
|
animation animation = (animation) animationutils.loadanimation(
animation2activity. this , r.anim.list_anim);
|
3)创建layoutanimationcontroller对象:
4)设置控件的显示顺序以及延迟时间
1
2
|
controller.setorder(layoutanimationcontroller.order_normal);
controller.setdelay( 0 .5f);
|
5)为listview设置layoutanimationcontroller属性:
完整代码:
1
2
3
4
5
6
7
8
9
10
11
|
private class buttonlistener implementsonclicklistener {
public void onclick(view v) {
listview.setadapter(createlistadapter());
animation animation = (animation) animationutils.loadanimation(
animation2activity. this , r.anim.list_anim);
layoutanimationcontroller controller = new layoutanimationcontroller(animation);
controller.setorder(layoutanimationcontroller.order_normal);
controller.setdelay( 0 .5f);
listview.setlayoutanimation(controller);
}
}
|
animationlistener
1、什么是animationlistener
1).animationlistener是一个监听器,该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法;
2).animationlistener主要包括如下三个方法:
① onanimationend(animation animation) - 当动画结束时调用
② onanimationrepeat(animation animation) - 当动画重复时调用
③ onaniamtionstart(animation animation) - 当动画启动时调用
2、具体实现
1)main.xml
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" ?>
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:id= "@+id/layout"
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" >
<button android:id= "@+id/addbutton"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:layout_alignparentbottom= "true"
android:text= "添加图片" />
<button android:id= "@+id/deletebutton"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:layout_above= "@id/addbutton"
android:text= "删除图片" />
<imageview android:id= "@+id/image"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_centerinparent= "true"
android:layout_margintop= "100dip"
android:src= "@drawable/an" />
</relativelayout>
|
2).java文件
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
public class animation2activity extends activity {
private button addbutton = null ;
private button deletebutton = null ;
private imageview imageview = null ;
private viewgroup viewgroup = null ;
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
addbutton = (button)findviewbyid(r.id.addbutton);
deletebutton = (button)findviewbyid(r.id.deletebutton);
imageview = (imageview)findviewbyid(r.id.image);
//linearlayout下的一组控件
viewgroup = (viewgroup)findviewbyid(r.id.layout);
addbutton.setonclicklistener(newaddbuttonlistener());
deletebutton.setonclicklistener(newdeletebuttonlistener());
}
private class addbuttonlistener implements onclicklistener{
public void onclick(view v) {
//淡入
alphaanimation animation = new alphaanimation( 0 .0f, 1 .0f);
animation.setduration( 1000 );
animation.setstartoffset( 500 );
//创建一个新的imageview
imageview newimageview = new imageview(
animation2activity. this );
newimageview.setimageresource(r.drawable.an);
viewgroup.addview(newimageview,
new layoutparams(
layoutparams.fill_parent,
layoutparams.wrap_content));
newimageview.startanimation(animation);
}
}
private class deletebuttonlistener implements onclicklistener{
public void onclick(view v) {
//淡出
alphaanimation animation = new alphaanimation( 1 .0f, 0 .0f);
animation.setduration( 1000 );
animation.setstartoffset( 500 );
//为aniamtion对象设置监听器
animation.setanimationlistener(
new removeanimationlistener());
imageview.startanimation(animation);
}
}
private class removeanimationlistener implements animationlistener{
//动画效果执行完时remove
public void onanimationend(animation animation) {
system.out.println( "onanimationend" );
viewgroup.removeview(imageview);
}
public void onanimationrepeat(animation animation) {
system.out.println( "onanimationrepeat" );
}
public void onanimationstart(animation animation) {
system.out.println( "onanimationstart" );
}
}
}
|
3、总结一下
可以在activity中动态添加和删除控件,方法是:
1)取到那个layout
2)添加时,先创建对象,然后添加
1
2
3
4
5
6
7
|
imageview newimageview = new imageview(
animation2activity. this );
newimageview.setimageresource(r.drawable.an);
viewgroup.addview(newimageview,
new layoutparams(
layoutparams.fill_parent,
layoutparams.wrap_content));
|
3)删除时,直接删除。
希望本文所述对大家android程序设计有所帮助。