Android 动画学习-01

本文深入探讨了Android动画系统的使用方法,包括Tweened动画、Frame-by-frame动画、动画集、插值器、帧动画控制器及动画监听器。通过示例代码展示了如何在XML和Java中实现各种动画效果,如透明度变化、旋转、缩放和平移,以及如何使用LayoutAnimationController统一多个视图的显示顺序。同时介绍了如何动态添加和删除控件,并在动画过程中添加和移除视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android Animation

Contents:


  • Animations

  • Tween Animations

  • AnimationSet

  • Interpolator

  • Frame-By-Frame Animations

  • LayoutAnimationsController

  • AnimationListener

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Animations

一、Animations介绍:

       Animations是一个实现Android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。


二、Animations的分类:

      Animations总体可分为两大类:

      1、Tweened Animations:(补间动画)提供了旋转、移动、伸展和淡入淡出等效果。

Alpha;淡入淡出 ----AlphaAnimation

Scale:缩放效果 ----ScaleAnimation

Rotate:旋转 ---- RotateAnimation

Translate:移动 ---- TranslateAnimation

      2、Frame-by-frame Animations:(逐帧动画),使用此动画可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示。


三、Animations的使用(代码)

      TweenedAnimations步骤:

      1、创建一个AnimationSet对象(Animation子类);

      2、根据功能,创建相应的Animation对象;

      3、根据项目的需求,为Animation对象设置相应的数据;

      4、将Animation对象添加到AnimationSet对象中;

      5、使用控件对象开始执行AnimationSet。

 四、 示例代码

      1.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:src="@drawable/aaa" />

    <Button
        android:id="@+id/btn_rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="旋转" />

    <Button
        android:id="@+id/btn_scale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_rotate"
        android:layout_below="@+id/btn_rotate"
        android:text="缩放" />

    <Button
        android:id="@+id/btn_translate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_scale"
        android:layout_below="@+id/btn_scale"
        android:text="平移" />

    <Button
        android:id="@+id/btn_alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_translate"
        android:layout_below="@+id/btn_translate"
        android:text="透明" />

</RelativeLayout>
     2. java文件

public class MainActivity extends Activity implements OnClickListener {

	private ImageView image;
	private AlphaAnimation alphaAi;
	private RotateAnimation rotateAi;
	private ScaleAnimation scaleAi;
	private TranslateAnimation translateAi;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		image = (ImageView)findViewById(R.id.image);
		findViewById(R.id.btn_alpha).setOnClickListener(this);
		findViewById(R.id.btn_translate).setOnClickListener(this);
		findViewById(R.id.btn_scale).setOnClickListener(this);
		findViewById(R.id.btn_rotate).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_alpha:
			//使控件变透明或不透明
			/**
			 * 1、创建一个AnimationSet对象,参数为Boolean类型:
			 *    传true意味着使用Animation的interpolator
			 *    传false则表示使用自己的
			 * 2、创建一个AlphaAnimation对象,参数从完全透明到完全不透明(1.0f ~ 0.0f)
			 * 3、设置动画执行的时间
			 * 4、将AlphaAnimation对象添加到AnimationSet当中
			 * 5、使用ImageView控件的startAnimation方法执行动画
			 */
			AnimationSet ai1 = new AnimationSet(true);
			alphaAi = new AlphaAnimation(0.0f, 1.0f);
			alphaAi.setDuration(2000);
			/** 常用方法 */
//			alphaAi.setRepeatCount(3);//设置重复次数
//			alphaAi.setFillAfter(true);//动画执行完后是否停留在执行完的状态
//			alphaAi.setStartOffset(1000);//执行前的等待时间 
			ai1.addAnimation(alphaAi);
			image.startAnimation(ai1);
			break;
		case R.id.btn_rotate:
			//使控件旋转
			/**
			 * 1、创建一个AnimationSet对象,参数为Boolean类型:
			 *    传true意味着使用Animation的interpolator
			 *    传false则表示使用自己的
			 * 2、创建RotateAnimation对象,参数说明
			 *    参数1:从什么角度开始旋转
			 *    参数2:转到什么角度停止
			 *    
			 *    后面几个参数用于设置围绕着旋转的圆的圆心在哪里
			 *    
			 *    参数3:确定x轴坐标的类型,
			 *          有ABSOLUT绝对坐标、
			 *          RELATIVE_TO_SELF相对于自身坐标、
			 *          RELATIVE_TO_PARENT相对于父控件的坐标
			 *    参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
			 *    参数5:确定y轴坐标的类型
			 *    参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
			 */
			AnimationSet ai2 = new AnimationSet(true);
			rotateAi = new RotateAnimation(0.0f, 360.0f, 
					Animation.RELATIVE_TO_SELF, 0.5f, 
					Animation.RELATIVE_TO_SELF, 0.5f);
			rotateAi.setDuration(2000);
			ai2.addAnimation(rotateAi);
			image.startAnimation(ai2);
			break;
		case R.id.btn_scale:
			//使控件缩放
			/**
			 * 参数1:x轴的初始值
			 * 参数2:x轴收缩后的值
			 * 参数3:y轴的初始值
			 * 参数4:y轴收缩后的值
			 * 参数5:确定x轴坐标的类型
			 * 参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
			 * 参数7:确定y轴坐标的类型
			 * 参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
			 */
			AnimationSet ai3 = new AnimationSet(true);
			scaleAi = new ScaleAnimation(0.0f, 0.1f, 1.0f, 1.1f,
					Animation.RELATIVE_TO_SELF, 0.5f, 
					Animation.RELATIVE_TO_SELF, 0.5f);
			scaleAi.setDuration(2000);
			ai3.addAnimation(scaleAi);
			image.startAnimation(scaleAi);
			break;
		case R.id.btn_translate:
			//使控件平移
			/**
			 * 参数1~2:x轴的开始位置
			 * 参数3~4:y轴的开始位置
			 * 参数5~6:x轴的结束位置
			 * 参数7~8:y轴的结束位置
			 */
			AnimationSet ai4 = new AnimationSet(true);
			translateAi = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.0f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			translateAi.setDuration(2000);
			ai4.addAnimation(translateAi);
			image.startAnimation(ai4);
			break;
		}
		
	}


}

Tween Animations的常用方法

      1、setDuration(long durationMills)  : 设置动画的执行时间(单位:毫秒)

      2、setFillAfter(Boolean fillAfter)  : true表示动画执行完毕后,控件将停留在执行结束的状态

      3、setFillBefore(Boolean fillBefore)  : true表示动画执行完毕后,控件讲回到动画之前的状态

      4、setStartOffSet(long startOffSet)  : 设置动画执行之前的等待时间

      5、setRepeatCount(int repeatCount)  : 设置动画重复执行的次数


动画除了可以在代码中使用,同时也可以在xml文件中使用,好处就是可维护性变高了,只不过没有在代码中易调试


一、在Xml中使用Animations步骤:

      1、在res文件夹下建立一个anim文件夹

      2、在anim文件夹下创建xml文件, 选择set标签的文件

      3、在set标签当中加入alpha、rotate、scale或者translate动画标签,并设置属性

      4、在代码当中使用AnimationUtils工具装载xml文件,并生成Animation对象。因为AnimationSet是Animation的子类,所向上转型,用Animation对象接受。


二、具体实现

      1、alpha_set.xml

<?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_set.xml

<?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_set.xml

<?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_set.xml

<?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文件中调用动画

public class MainActivity extends Activity implements OnClickListener {

	private ImageView image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		image = (ImageView)findViewById(R.id.image);
		findViewById(R.id.btn_alpha).setOnClickListener(this);
		findViewById(R.id.btn_translate).setOnClickListener(this);
		findViewById(R.id.btn_scale).setOnClickListener(this);
		findViewById(R.id.btn_rotate).setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_alpha:
			//使控件变透明或不透明
			Animation ai1 = AnimationUtils.loadAnimation(this, R.anim.alpha_set);
			image.startAnimation(ai1);
			break;
		case R.id.btn_rotate:
			//使控件旋转
			Animation ai2 = AnimationUtils.loadAnimation(this, R.anim.rotate_set);
			image.startAnimation(ai2);
			break;
		case R.id.btn_scale:
			//使控件缩放
			Animation ai3 = AnimationUtils.loadAnimation(this, R.anim.scale_set);
			image.startAnimation(ai3);
			break;
		case R.id.btn_translate:
			//使控件平移
			Animation ai4 = AnimationUtils.loadAnimation(this, R.anim.translate_set);
			image.startAnimation(ai4);
			break;
		}
		
	}

}

AnimationSet的具体使用方法

1、AnimationSet是Animation的子类

2、一个AnimationSet包含了一系列的Animation

3、针对AnimationSet设置一些Animation的常见属性(如startOffset、duration等),可以被包含在AnimationSet当中的Animation集成


例如:一个AnimationSet中有两个Animation,效果叠加

一、在xml文件中使用

double_animation.xml

<?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>


在代码中调用
<pre name="code" class="java">// 使用AnimationUtils装载动画配置文件

           Animation animation = AnimationUtils.loadAnimation(

                  Animation2Activity.this, R.anim. doubleani);

           // 启动动画

           image.startAnimation(animation);

 

 2.在java文件中使用 

           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标签中使用:

<set xmlns:android="http://schemas.android.com/apk/res/android"

     android:interpolator="@android:anim/accelerate_interpolator"/>

2、如果在一个set标签中包含多个效果,如果想让这些动画效果共享一个Interpolator。

     android:shareInterpolator="true"

3、如果不行共享一个Interpolator,则设置android:shareInterpolator="false",并且需要在每一个动画效果处添加interpolator。

<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。

AnimationSet animationSet = newAnimationSet(true);

animationSet.setInterpolator( new   AccelerateInterpolator()) ;

5、如果不设置共享一个Interpolator则可以在每一个Animation对象上面设置Interpolator。

AnimationSet animationSet = newAnimationSet(false);

alphaAnimation.setInterpolator(new AccelerateInterpolator());

rotateAnimation.setInterpolator( new   DecelerateInterpolator());


Frame-By-Frame Animations的使用方法

帧动画是一帧一帧的格式显示动画效果。

1、main.xml文件

<?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>

2、anim.xml

<?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>

3、java文件

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();

       }

    }

}

LayoutAnimationController

1、LayoutAnimationController是什么

      LayoutAnimationController可以用于实现使多个控件按顺序一个一个的显示。

      1)LayoutAnimationController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置统一的动画效果。

      2)每一个控件都有相同的动画效果

      3)控件的动画效果可以在不同的时间显示出来

      4)LayoutAnimationController可以在xml文件当中设置,也可以在代码当中进行设置

2、在xml当中使用LayoutAnimationController

      1)res/anim文件夹下创建一个名为list_anim_layout.xml文件:

    android;delay   ---         动画间隔时间;子类动画时间间隔(或者延迟), 70% 也可以是一个浮点数如“1.2f”等

     android:animationOrder  ----  动画执行的顺序(normal: 顺序执行;  random: 随机执行;  reverse: 反向显示

     android:animation  ----  引用动画效果文件

<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文件,设置动画效果

<?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添加如下配置

<?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 >

      4)项目结构

      5) item.xml

<?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 >

      6)java文件代码

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());

       }

    }

}

备注:    要将整个动画效果设置到LinearLayout中,可以做如下的设置

<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的构造方法创建Animatior对象;

Animation animation = (Animation) AnimationUtils.loadAnimation(

                  Animation2Activity. this , R.anim. list_anim ) ;

      3)创建LayoutAnimationController对象:

LayoutAnimationController controller = new LayoutAnimationController(animation);

      4)设置控件的显示顺序以及延迟时间

controller.setOrder(LayoutAnimationController.ORDER_NORMAL); 

controller.setDelay(0.5f) ;

      5)为ListView设置LayoutAnimationController属性:

listView.setLayoutAnimation(controller);

      附完整代码:

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主要包含如下三个方法:

               1)onAnimationEnd(Animation  animation)  ----  当动画结束时调用

               2)onAnimationRepeat(Animation animation)  ------  当动画重复时调用

               3)onAnimationStart(Animation  animation)  ------  当动画启动时调用

2、具体实现

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

viewGroup = (ViewGroup)findViewById(R.id.layout);

    2)动态添加控件之前,需要先创建对象,然后添加

ImageView newImageView = new ImageView(

              Animation2Activity.this);

newImageView.setImageResource(R.drawable.an);

viewGroup.addView(newImageView,

              new LayoutParams(

                  LayoutParams.FILL_PARENT,

                  LayoutParams. WRAP_CONTENT )) ;

    3)删除

viewGroup.removeView(imageView);


写在后面:

转载自http://www.cnblogs.com/qiengo/archive/2012/05/03/2480386.html

并做了部分的修改。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值