Just like the button control has the listener, the animation effect also has the listener, only then realizes the Animationlistener to be able to realize to the animation effect listens, in which needs to overload three functions, is the following several functions:
Copy Code code as follows:
Private class Mylistenr implements animationlistener{
@Override
public void Onanimationend (Animation arg0) {
TODO auto-generated Method Stub
}
@Override
public void Onanimationrepeat (Animation arg0) {
TODO auto-generated Method Stub
}
@Override
public void Onanimationstart (Animation arg0) {
TODO auto-generated Method Stub
}
}
The first function means what the developer needs to do after the animation is finished, and the second function means what should be done during the animation's repetition, and the third function means what happens when the animation starts to execute.
Below I implemented an example, click the Delete button, the picture slowly fade, and eventually deleted, when the Click Add button to add a ImageView to the viewgroup, the implementation of the screenshot is as follows:
The specific implementation code is as follows:
Copy Code code as follows:
public class Mainactivity extends activity {
Private button button;
Private Button button2;
Private ImageView ImageView;
Private ViewGroup ViewGroup;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
button= (Button) Findviewbyid (R.id.button_add);
Button2= (Button) Findviewbyid (r.id.button_delete);
imageview= (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);
viewgroup= (ViewGroup) Findviewbyid (R.id.viewgroup);
Button.setonclicklistener (New Mybutton ());
Button2.setonclicklistener (New Mybutton ());
}
Private class Mybutton implements onclicklistener{
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.id.button_add:
ADD ();
Break
Case R.id.button_delete:
Delete ();
Break
Default
Break
}
}
}
public void Add () {
Alphaanimation alphaanimation=new alphaanimation (0.0f, 1.0f);
Alphaanimation.setduration (2000);
Alphaanimation.setstartoffset (500);
ImageView imageviewadd=new ImageView (mainactivity.this);
Imageviewadd.setimageresource (R.drawable.ic_launcher);
Viewgroup.addview (Imageviewadd);
Viewgroup.addview (Imageviewadd, New Layoutparams (
Layoutparams.wrap_content,layoutparams.wrap_content));
Imageviewadd.startanimation (alphaanimation);
}
public void Delete () {
Alphaanimation alphaanimation=new alphaanimation (1.0f, 0.0f);
Alphaanimation.setduration (2000);
Alphaanimation.setstartoffset (500);
Imageview.startanimation (alphaanimation);
Alphaanimation.setanimationlistener (New Mylistenr ());
}
Private class Mylistenr implements animationlistener{
@Override
public void Onanimationend (Animation arg0) {
TODO auto-generated Method Stub
Viewgroup.removeview (ImageView);
LOG.D ("Brucezhang", "Animation end!");
}
@Override
public void Onanimationrepeat (Animation arg0) {
TODO auto-generated Method Stub
LOG.D ("Brucezhang", "Animation repeat!");
}
@Override
public void Onanimationstart (Animation arg0) {
TODO auto-generated Method Stub
LOG.D ("Brucezhang", "Animation start!");
}
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}
The layout file for this instance is as follows, note that you need to give the ID of the viewgroup under the root tag:
Copy Code code as follows:
<absolutelayout 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"
Tools:context= ". Mainactivity "
Android:id= "@+id/viewgroup"
>
<button
Android:id= "@+id/button_add"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_x= "0DP"
Android:layout_y= "367DP"
android:text= "Add Picture"/>
<button
Android:id= "@+id/button_delete"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_x= "0DP"
Android:layout_y= "410DP"
android:text= "Delete Picture"/>
<imageview
Android:id= "@+id/imageview1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_x= "153DP"
Android:layout_y= "155DP"
android:src= "@drawable/ic_launcher"/>
</AbsoluteLayout>