Android development-Animation (5), androidanimation

Source: Internet
Author: User

Android development-Animation (5), androidanimation
Use of Animation in android development (5)This blog post mainly describes how to use AnimationLisenter in Animation and how to call such life cycle functions. The code example is as follows:

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 );

}

// Delete the imageView Control
Class RemoveButtonListener implements OnClickListener {


@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Create an animation object that fades out.
AlphaAnimation alphaAnimation = new AlphaAnimation (1.0f, 0.0f );
AlphaAnimation. setDuration (2000 );
// Set the listener for the Animation object
AlphaAnimation. setAnimationListener (new RemoveAnimationListener ());
ImageView. startAnimation (alphaAnimation );

}

}



// Add the ImageView Control
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 an ImageView control in the current Activity
ImageView addImageView = new ImageView (MainActivity. this );

// Set ID for ImageView
AddImageView. setId (R. id. myImage );

// Set image resources for the ImageView Control
AddImageView. setImageResource (R. drawable. ic_launcher );

// ViewGroup indicates the entire layout in main. xml.
// Add the imageView to the layout
ViewGroup. addView (addImageView, new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT ));
// AlphaAnimation. setAnimationListener (new addAnimationListener ());

ImageView = addImageView;

AddImageView. startAnimation (alphaAnimation );

}

}

// The AnimationLinstener is used to call the relevant functions in the Animation effect period, such as the start and end of the Animation effect.
Class RemoveAnimationListener implements AnimationListener {


// Call this method after the Animation effect ends.
@ Override
Public void onAnimationEnd (Animation animation ){
// TODO Auto-generated method stub
System. out. println ("Animation end ");
// Delete the ImageView from the Layout
ViewGroup. removeView (imageView );
}


// Call this method when the animation effect is repeated.
@ Override
Public void onAnimationRepeat (Animation animation ){
// TODO Auto-generated method stub
System. out. println ("Animation repeat ");
}

// Call this method when the animation effect starts to be executed.
@ 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. 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>

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.