Android Development Animation (Fri)

Source: Internet
Author: User

the animation of Android developmentThe use(v) This blog is mainly about the use of animationlisenter in animation, and the invocation of some life-cycle functions of this class, with the following code examples:

Mainactivity.java:
Package Com.example.animationlistener;


Import Android.os.Bundle;
Import android.app.Activity;
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.AlphaAnimation;
Import android.view.animation.Animation;
Import Android.view.animation.Animation.AnimationListener;
Import Android.widget.Button;
Import Android.widget.ImageView;

public class Mainactivity extends Activity {


Private ViewGroup viewgroup = null;
Private Button AddButton = null;
Private Button RemoveButton = null;
Private ImageView ImageView = null;

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

AddButton = (Button) Findviewbyid (R.id.addbutton);
Addbutton.setonclicklistener (New Addbuttonlistener ());

RemoveButton = (Button) Findviewbyid (R.id.removebutton);
Removebutton.setonclicklistener (New Removebuttonlistener ());

ViewGroup = (viewgroup) Findviewbyid (r.id.layout_id);

ImageView = (ImageView) Findviewbyid (r.id.myimage);

}

Remove ImageView Control
Class Removebuttonlistener implements onclicklistener{


@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Create an animated object with fade-out effect
Alphaanimation alphaanimation = new Alphaanimation (1.0f, 0.0f);
Alphaanimation.setduration (2000);
Setting up listeners for animation objects
Alphaanimation.setanimationlistener (New Removeanimationlistener ());
Imageview.startanimation (alphaanimation);

}

}



Adding ImageView controls
Class Addbuttonlistener implements onclicklistener{

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Alphaanimation alphaanimation = new Alphaanimation (0.0f, 1.0f);

Alphaanimation.setduration (2000);

Create a ImageView control in the current activity
ImageView Addimageview = new ImageView (mainactivity.this);

Set the ID for ImageView
Addimageview.setid (R.id.myimage);

To set a picture resource for a ImageView control
Addimageview.setimageresource (R.drawable.ic_launcher);

ViewGroup represents the entire layout in the Main.xml
Add ImageView to the layout
Viewgroup.addview (Addimageview, New Layoutparams (Layoutparams.fill_parent, layoutparams.wrap_content));
Alphaanimation.setanimationlistener (New Addanimationlistener ());

ImageView = Addimageview;

Addimageview.startanimation (alphaanimation);

}

}

Animationlinstener is called when the animation animation effect starts and ends, and the correlation function is invoked.
Class Removeanimationlistener implements animationlistener{


Call this method when the animation effect ends
@Override
public void Onanimationend (Animation Animation) {
TODO auto-generated Method Stub
System.out.println ("Animation end");
To remove ImageView from the layout
Viewgroup.removeview (ImageView);
}


This method is called when the animation effect executes repeatedly
@Override
public void Onanimationrepeat (Animation Animation) {
TODO auto-generated Method Stub
System.out.println ("Animation repeat");
}

This method is called when the animation effect starts executing
@Override
public void Onanimationstart (Animation Animation) {
TODO auto-generated Method Stub
System.out.println ("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.main, menu);
return true;
}


}


Main layout file Main.xml: <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/layout_id"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">


<textview
Android:id= "@+id/mytext"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>

<imageview
Android:id= "@+id/myimage"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:src= "@drawable/ic_launcher"
android:layout_below= "@id/mytext"
/>

<button
Android:id= "@+id/addbutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/myimage"
android:text= "Add Image"

/>

<button
Android:id= "@+id/removebutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/addbutton"
android:text= "Remove Image"
/>

</RelativeLayout>

Android Development Animation (Fri)

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.