First, Code
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"3Android:id= "@+id/layoutid"4android:orientation= "vertical" android:layout_width= "fill_parent"5android:layout_height= "Fill_parent" >6<button android:id= "@+id/addbuttonid" android:layout_width= "Fill_parent"7android:layout_height= "Wrap_content" android:layout_alignparentbottom= "true"8android:text= "Add Picture"/>9 Ten<button android:id= "@+id/removebuttonid" android:layout_width= "Fill_parent" Oneandroid:layout_height= "Wrap_content" android:layout_above= "@id/addbuttonid" Aandroid:text= "Delete Picture"/> - - the<imageview android:id= "@+id/imageviewid" -Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" -Android:layout_centerinparent= "true" android:layout_margintop= "100dip" -android:src= "@drawable/a1"/> +</RelativeLayout>
2.java
(1) Mainactivity.java
1 PackageCom.animationlistener;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.View;6 ImportAndroid.view.View.OnClickListener;7 ImportAndroid.view.ViewGroup;8 ImportAndroid.view.ViewGroup.LayoutParams;9 Importandroid.view.animation.AlphaAnimation;Ten Importandroid.view.animation.Animation; One ImportAndroid.view.animation.Animation.AnimationListener; A ImportAndroid.widget.Button; - ImportAndroid.widget.ImageView; - the Public classMainactivityextendsActivity { - - PrivateButton RemoveButton =NULL; - PrivateButton AddButton =NULL; + PrivateImageView ImageView =NULL; - PrivateViewGroup ViewGroup =NULL; + A @Override at protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - -RemoveButton =(Button) Findviewbyid (R.id.removebuttonid); -Removebutton.setonclicklistener (NewRemovebuttonlistener ()); inAddButton =(Button) Findviewbyid (R.id.addbuttonid); -Addbutton.setonclicklistener (NewAddbuttonlistener ()); to +ImageView =(ImageView) Findviewbyid (R.ID.IMAGEVIEWID); -ViewGroup =(ViewGroup) Findviewbyid (R.id.layoutid); the } * $ classAddbuttonlistenerImplementsOnclicklistener {Panax Notoginseng @Override - Public voidOnClick (View v) { theAnimation Animation =NewAlphaanimation (0.0f, 1.0f); +Animation.setduration (1000); AAnimation.setstartoffset (500); theImageView Imageviewadd =NewImageView (mainactivity. This); + Imageviewadd.setimageresource (r.drawable.a1); -Viewgroup.addview (Imageviewadd,Newlayoutparams (layoutparams.match_parent, layoutparams.wrap_content)); $ imageviewadd.startanimation (animation); $ } - } - the classRemovebuttonlistenerImplementsOnclicklistener { - @OverrideWuyi Public voidOnClick (View v) { theAnimation Animation =NewAlphaanimation (1.0f, 0.0f); -Animation.setduration (1000); WuAnimation.setstartoffset (500); -Animation.setanimationlistener (NewRemoveanimationlistener ()); About imageview.startanimation (animation); $ } - } - - classRemoveanimationlistenerImplementsAnimationlistener { A + @Override the Public voidOnanimationstart (Animation Animation) { -System.out.println ("Start"); $ } the the @Override the Public voidonanimationend (Animation Animation) { theSystem.out.println ("End"); - Viewgroup.removeview (imageView); in } the the @Override About Public voidonanimationrepeat (Animation Animation) { theSystem.out.println ("Repeat"); the } the } +}
Android_mars Learning Note _s02_012_animation_ using Animationlistener to remove or add components at the end of an animation