Android Animation Listener Animationlistener method of use) _android

Source: Internet
Author: User
Tags stub xmlns

Animationlistener listen to the name of the animation set up the listener, said the simple point is that the animation effect before the start of execution, execution and repeated execution can trigger the listener, so that the corresponding function.

The development environment is android4.1.
The main use of Animaitonlistener is to set up a listener on the animation, that is, to use the animation method member Setanimationlistener (). Its parameter is the function of the listener.
Now to say the function of the experiment, there are mainly 2 buttons, one is to increase the picture of the button, one is to delete the picture button, there is a ImageView control, used to display pictures. When the button that increases the picture is pressed, the picture will appear with no to full-size size change, and when the delete button is pressed, the picture will gradually exit from full size to 0 size, and then remove it.

The program interface is as follows:

Here is worth mentioning is viewgroup this control, the feeling is the layout control, this experiment picture control imageview inside the picture of the increase and deletion is the use of Viewgrop AddView () and Removeview () method. The parameters passed in these 2 methods are imageview.
In addition, the Mars teacher data in the Add Picture listener function, redefined a imageview, the ImageView added to the ViewGroup, which will lead to a problem, that is, when we delete the picture and then reload after the deletion, Because we delete the ImageView in the layout file, but the Add button is another imageview, so we delete the ImageView in the layout file, but the picture will be displayed on the screen. So the solution is to use the ImageView in the layout file directly in the Add button function, so that the program can always add pictures and delete pictures, and in the screen can also see the effect.

The main program code is as follows:
Mainactivity.java:

Copy Code code as follows:

Package com.example.anim_5;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import Android.view.ViewGroup.LayoutParams;
Import android.view.animation.Animation;
Import Android.view.animation.Animation.AnimationListener;
Import android.view.animation.ScaleAnimation;
Import Android.widget.Button;
Import Android.widget.ImageView;

public class Mainactivity extends activity {

Private Button add = null;
Private Button delete = null;
Private ViewGroup viewgroup = null;
Private ImageView ImageView = null;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Add = (Button) Findviewbyid (R.id.add);
Delete = (Button) Findviewbyid (r.id.delete);
ImageView = (ImageView) Findviewbyid (r.id.image);
ViewGroup = (viewgroup) Findviewbyid (r.id.main_layout);

Add.setonclicklistener (New Addonclicklistener ());
Delete.setonclicklistener (New Deleteonclicklistener ());
}

Private class Addonclicklistener implements onclicklistener{

public void OnClick (View v) {
TODO auto-generated Method Stub
All numbers must be followed by F, otherwise the error
scaleanimation animation = new Scaleanimation (0.0f, 1.0f, 0.0f, 1.0f,
Animation.relative_to_self, 0.5f,
Animation.relative_to_self, 0.5f);
Animation.setstartoffset (500);
Animation.setduration (1000);
ImageView Image_add = new ImageView (mainactivity.this);
Image_add.setimageresource (R.drawable.london_olympic);
Viewgroup.addview (Image_add, New Layoutparams (Layoutparams.wrap_content, layoutparams.wrap_content));
Image_add.setanimation (animation);
or directly with the layout file in the ImageView better, otherwise added to the picture with the following method is not visually erased
Here is the Setimageresource method to load the picture onto the ImageView control.
Imageview.setimageresource (R.drawable.london_olympic);
Loading ImageView into an activity with ViewGroup
Viewgroup.addview (ImageView, New Layoutparams (Layoutparams.wrap_content, layoutparams.wrap_content));
Start the ImageView animation
Imageview.startanimation (animation);
}

}

Private class Deleteonclicklistener implements onclicklistener{

public void OnClick (View v) {
TODO auto-generated Method Stub
scaleanimation animation = new Scaleanimation (1.0f, 0.0f, 1.0f, 0.0f,
Animation.relative_to_self, 0.5f,
Animation.relative_to_self, 0.5f);
Animation.setstartoffset (500);
Animation.setduration (1000);
Set Animationlistener
Animation.setanimationlistener (New Deleteanimationlistener ());
Imageview.startanimation (animation);
}
}

Private class Deleteanimationlistener implements animationlistener{

public void Onanimationend (Animation Animation) {
TODO auto-generated Method Stub
Viewgroup.removeview (ImageView);
}

public void Onanimationrepeat (Animation Animation) {
TODO auto-generated Method Stub

}

public void Onanimationstart (Animation Animation) {
TODO auto-generated Method Stub

}

}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}



Activity_main.xml:
Copy Code code as follows:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/main_layout"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >

<button
Android:id= "@+id/delete"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_alignparentbottom= "true"
android:text= "Delete Image"
/>

<button
Android:id= "@+id/add"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:layout_above= "@+id/delete"
android:text= "Add Image"/>
<imageview
Android:id= "@+id/image"
Android:layout_width= "Wrap_content"
android:layout_height= "310dip"
android:src= "@drawable/london_olympic"
/>

</RelativeLayout>



Conclusion: Through this experiment, we can understand the basic use of animationlistener.

Author: tornadomeet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.