AnimationListener的使用方法
1.AnimationListener是一个监听器,该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法;
2.AnimationListener主要包括如下三个方法:
·onAnimationEnd(Animation animation) - 当动画结束时调用
·onAnimationRepeat(Animation animation) - 当动画重复时调用
·onAniamtionStart(Animation animation) - 当动画启动时调用
直接上demo测试程序:实现的功能是:当要删除图片时,图片会慢慢的渐隐消去;当图片添加进来是,图片慢慢的显示出来
AnimationListenerActivity.java
package com.example.androidanimationlistenerdemos;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.widget.Button; import android.widget.ImageView;public class AnimationListenerActivity extends Activity {private Button addButton = null;private Button deleteButton = null;private ImageView imageView = null;private ViewGroup viewGroup = null;@Overridepublic 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(new AddButtonListener());deleteButton.setOnClickListener(new DeleteButtonListener());}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(* AnimationListenerActivity.this);* newImageView.setImageResource(R.drawable.image);* viewGroup.addView(newImageView, new LayoutParams(* LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));* newImageView.startAnimation(animation);*/imageView = new ImageView(AnimationListenerActivity.this);imageView.setImageResource(R.drawable.image);viewGroup.addView(imageView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));imageView.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 {// 动画效果执行完时removepublic 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");}} }
布局文件xml:main.xml
<?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/image" /> </RelativeLayout>
drawable[-***]目录下放一张image.png图片
demo 下载:http://pan.baidu.com/s/1gdnGswj