Android: Animation
Animation is divided into two types: gradient animation tweened animation and frame-by-frame Animation. A gradient animation uses a frame of image to produce four effects: 1, alpha, 2, rotate, 3, scale, 4, and translates. This can be achieved through code or xml files. Frame-by-frame animation is a set of images that can be played quickly. There are also some Animationset, AnimationListener, LayoutAnimationController, which are interspersed in the instance.
Example 1: Code Implementation of tweened animation
① Create a layout file: add an ImageView to the four buttons
② Activity
Package com. example. f_animation_tween; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. view. view; import android. view. view. onClickListener; import android. view. animation. alphaAnimation; import android. view. animation. animation; import android. view. animation. animation. animationListener; import android. view. animation. animationSet; import android. view. animation. rotateAnimation; import android. view. animation. scaleAnimation; import android. view. animation. translateAnimation; import android. widget. button; import android. widget. imageView; public class MainActivity extends ActionBarActivity {private Button button_alpha, button_rotate, button_scale, button_trans; private ImageView imageView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button_alpha = (Button) findViewById (R. id. button_alpha); button_rotate = (Button) findViewById (R. id. button_rotate); button_scale = (Button) findViewById (R. id. button_scale); button_trans = (Button) findViewById (R. id. button_translate); imageView = (ImageView) findViewById (R. id. imageview); button_alpha.setOnClickListener (new ButtonAlphaListerner (); listener (new ButtonRotateListerner (); listener (new ButtonScaleListerner (); new ButtonTransListerner ());} private class ButtonAlphaListerner implements OnClickListener {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub // create an AlphaAnimation, the constructor parameter indicates the fading duration of AlphaAnimation alphaAnimation = new AlphaAnimation (1, 0) from 1 to 0; // The fading duration is 2 seconds. setDuration (2000); // Repeat twice and add alphaAnimation three times. setRepeatCount (2); // sets a listener alphaAnimation. setAnimationListener (new AnimationListener () {@ Overridepublic void onAnimationStart (Animation arg0) {// TODO Auto-generated method stubSystem. out. println ("---> start") ;}@ Overridepublic void onAnimationRepeat (Animation arg0) {// TODO Auto-generated method stubSystem. out. println ("---> repeat") ;}@ Overridepublic void onAnimationEnd (Animation arg0) {// TODO Auto-generated method stubSystem. out. println ("---> end") ;}}); // fades the image into an animated imageView. startAnimation (alphaAnimation); // other controls can also be used to generate Animations (alphaAnimation);} private class ButtonRotateListerner implements OnClickListener {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub // rotate the animation. The center of the animation is obtained by the constructor: // The third four parameters represent the maximum X axis relative to itself, and the Y axes of five or six parameters are the same, it is equivalent to the Animation in the lower-right corner of the image. RELATIVE_TO_PARENT is relative to the parent control RotateAnimation rotateAnimation = new RotateAnimation (0,360, Animation. RELATIVE_TO_SELF, 1, Animation. RELATIVE_TO_SELF, 1); rotateAnimation. setDuration (2000); imageView. startAnimation (rotateAnimation);} private class ButtonScaleListerner implements OnClickListener {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stub // both horizontally and vertically reduce to the original 0.1, and the preceding points are the same as ScaleAnimation scaleAnimation = new ScaleAnimation (1, 0.1f, 1, 0.1f, animation. RELATIVE_TO_SELF, 1, Animation. RELATIVE_TO_SELF, 1); scaleAnimation. setDuration (2000); // set it to the final scaleAnimation. setFillAfter (true); imageView. startAnimation (scaleAnimation);} private class implements OnClickListener {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method implements alphaAnimation = new AlphaAnimation (1, 0 ); translateAnimation translateAnimation = new TranslateAnimation (Animation. RELATIVE_TO_PARENT, 0, Animation. RELATIVE_TO_PARENT, 1, Animation. RELATIVE_TO_SELF, 0, Animation. RELATIVE_TO_SELF, 0); AnimationSet animationSet = new AnimationSet (true); // you can add multiple Animation effects using an AnimationSet, which is the subclass of animationSet. addAnimation (translateAnimation); animationSet. addAnimation (alphaAnimation); animationSet. setDuration (2000); imageView. startAnimation (animationSet );}}}
Example 2: use XML to implement gradient Animation
① Create an anim folder under the res folder and add an alpha. xml file
② Activity main code
private class ButtonAlphaListerner implements OnClickListener {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubAnimation alphaAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);imageView.startAnimation(alphaAnimation);}}
For batch modification, the xml method is faster. Of course, you can add several animations to the xml file. You can check the parameters online when necessary. A framework is provided here.
Example 3: frame-by-frame animation
① Put several images in the drawable folder and create a new anim. xml file.
a
② Activity main code
Private class ButtonListener implements OnClickListener {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubimageView. setBackgroundResource (R. drawable. anim); AnimationDrawable animationDrawable = (AnimationDrawable) imageView. getBackground (); // The AnimationDrawable class is used to implement the isStart =! IsStart; if (isStart) animationDrawable. start (); elseanimationDrawable. stop ();}}
Example 4: LayoutAnimationController
A layout animation controller is used to animated a layout's, or a view group's, children. each child uses the same animation but for every one of them, the animation starts at a different time.
ListView also inherits ViewGroup, so we use it as an example to write a Demo.
① Layout File
② Create an anim folder under the res folder and create two files: anim. xml and anim_layout.xml.
Set the animation effect, execution sequence, and interval in the preceding xml file.
③ Activity
Package com. example. f_animation_layoutcon; import java. util. arrayList; import java. util. list; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. view. animation. animation; import android. view. animation. animationUtils; import android. view. animation. layoutAnimationController; import android. widget. adapter; import android. widget. arrayAdapter; import android. widget. button; import android. widget. listView; public class MainActivity extends ActionBarActivity {private Button button; private ListView listView; private ArrayAdapter
Adapter; private List
List; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button); listView = (ListView) findViewById (R. id. listview); list = new ArrayList
(); List. add ("Beijing"); list. add ("Shanghai"); list. add ("Guangzhou"); adapter = new ArrayAdapter
(MainActivity. this, android. r. layout. simple_list_item_1, list); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubAnimation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. anim); LayoutAnimationController lac = new LayoutAnimationController (animation); lac. setOrder (LayoutAnimationController. ORDER_RANDOM); lac. setDelay (1.0f); listView. setLayoutAnimation (lac); listView. setAdapter (adapter); // You can also specify the layoutAnimation in the listview directly. // Add the property android: layoutAnimation = "@ anim/anim_layout"} to the ListView "}});}}
After you click "Button", the projects of the ListView are displayed one by one instead of all.
Summary: 1. Two implementation methods of tween animation, XML is more useful for batch modification, and event listener. 2. frame-by-frame animation. 3. LayoutAnimationController also has two implementation methods.