一、代码
1.xml
(1)activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/layoutId" 4 android:orientation="vertical" android:layout_width="fill_parent" 5 android:layout_height="fill_parent"> 6 <Button android:id="@+id/addButtonId" android:layout_width="fill_parent" 7 android:layout_height="wrap_content" android:layout_alignParentBottom="true" 8 android:text="添加图片" /> 9 10 <Button android:id="@+id/removeButtonId" android:layout_width="fill_parent" 11 android:layout_height="wrap_content" android:layout_above="@id/addButtonId" 12 android:text="删除图片" /> 13 14 15 <ImageView android:id="@+id/imageViewId" 16 android:layout_width="wrap_content" android:layout_height="wrap_content" 17 android:layout_centerInParent="true" android:layout_marginTop="100dip" 18 android:src="@drawable/a1" /> 19 </RelativeLayout>
2.java
(1)MainActivity.java
1 package com.animationlistener; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.view.ViewGroup; 8 import android.view.ViewGroup.LayoutParams; 9 import android.view.animation.AlphaAnimation; 10 import android.view.animation.Animation; 11 import android.view.animation.Animation.AnimationListener; 12 import android.widget.Button; 13 import android.widget.ImageView; 14 15 public class MainActivity extends Activity { 16 17 private Button removeButton = null; 18 private Button addButton = null; 19 private ImageView imageView = null; 20 private ViewGroup viewGroup = null; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 27 removeButton = (Button)findViewById(R.id.removeButtonId); 28 removeButton.setOnClickListener(new RemoveButtonListener()); 29 addButton = (Button)findViewById(R.id.addButtonId); 30 addButton.setOnClickListener(new AddButtonListener()); 31 32 imageView = (ImageView)findViewById(R.id.imageViewId); 33 viewGroup = (ViewGroup) findViewById(R.id.layoutId); 34 } 35 36 class AddButtonListener implements OnClickListener { 37 @Override 38 public void onClick(View v) { 39 Animation animation = new AlphaAnimation(0.0f, 1.0f); 40 animation.setDuration(1000); 41 animation.setStartOffset(500); 42 ImageView imageViewAdd = new ImageView(MainActivity.this); 43 imageViewAdd.setImageResource(R.drawable.a1); 44 viewGroup.addView(imageViewAdd, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 45 imageViewAdd.startAnimation(animation); 46 } 47 } 48 49 class RemoveButtonListener implements OnClickListener { 50 @Override 51 public void onClick(View v) { 52 Animation animation = new AlphaAnimation(1.0f, 0.0f); 53 animation.setDuration(1000); 54 animation.setStartOffset(500); 55 animation.setAnimationListener(new RemoveAnimationListener()); 56 imageView.startAnimation(animation); 57 } 58 } 59 60 class RemoveAnimationListener implements AnimationListener { 61 62 @Override 63 public void onAnimationStart(Animation animation) { 64 System.out.println("Start"); 65 } 66 67 @Override 68 public void onAnimationEnd(Animation animation) { 69 System.out.println("End"); 70 viewGroup.removeView(imageView); 71 } 72 73 @Override 74 public void onAnimationRepeat(Animation animation) { 75 System.out.println("Repeat"); 76 } 77 } 78 }