Android and frame-by-frame animation:
:
When we click the button, the image will not stop moving, and when we click the button again, it will stop in the current status.
Or
At the beginning, images will not stop moving.
Activity Code:
Package COM. NJ. test; import android. app. activity; import android. graphics. drawable. animationdrawable; import android. OS. bundle; import android. view. view; import android. widget. imageview;/***** @ description frame-by-frame animation in Android. * The principle of frame-by-frame animation is very simple. Like playing a movie, a picture similar to one another is constantly switched. When the switching speed reaches a certain value, * our vision will have a defective image, the appearance of the Shadow ensures the continuity of visual changes. At this time, the switching of images is the same as that in our eyes. * To use frame-by-frame animation: * Step 1: Create an XML file in the Res/drawable folder, this file defines in detail the images used for animation playback, the time used for switching each image *, and whether the video is played continuously. (Some articles say that placing the file in the Res/anim folder turns out to be wrong.) * Step 2: layout the file in the code, assign a value to a specific image display control, such as the imageview in this example. * Step 3: Use imageview. getbackground () gets the corresponding animationdrawable object, and then controls the animation through the method of this object */public class examplesactivity extends activity {private imageview imageviewnumber, imageviewrole; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); imageviewnumber = (imageview) findviewbyid (R. id. imageview_animation1); imageviewnumber. setbackgrou Ndresource (R. drawable. number_animation_drawable); imageviewrole = (imageview) findviewbyid (R. id. imageview_role); imageviewrole. setbackgroundresource (R. drawable. role_animation_drawable);} public void myclickhandler (view targetbutton) {// get the animationdrawable object animationdrawable = (animationdrawable) imageviewnumber. getbackground (); animationdrawable animationdrawable2 = (animationd Rawable) imageviewrole. getbackground (); // whether the animation is running if (animationdrawable. isrunning () {// stop the animation playing animationdrawable. Stop ();} else if (! Animationdrawable. isrunning () {// start or resume animation playback. start () ;}// whether the animation is running if (animationdrawable2.isrunning () {// stop the animation playing animationdrawable2.stop ();} else if (! Animationdrawable2.isrunning () {// start or continue playing the animation. animationdrawable2.start () ;}/ *** start motion */Public void onwindowfocuschanged (Boolean hasfocus) {// obtain the animationdrawable object animationdrawable animation1 = (animationdrawable) imageviewnumber. getbackground (); animationdrawable animation2 = (animationdrawable) imageviewrole. getbackground (); super. onwindowfocuschanged (hasfocus); If (hasfocus) {animation1.start (); animation2.start () ;}else {animation1.stop (); animation2.stop ();}}}
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "vertical"> <button Android: id = "@ + ID/button_animation1" Android: text = "animation start" Android: layout_gravity = "center_horizontal" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: onclick = "myclickhandler"> </button> <imageview Android: Id = "@ + ID/imageview_animation1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_weight = "1"> </imageview> <imageview Android: Id = "@ + ID/imageview_role" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margintop = "50dp"/> </linearlayout>
Number_animation_drawable.xml
<? XML version = "1.0" encoding = "UTF-8"?> <! -- The root tag is animation-list. oneshot indicates whether to display the animation only once. If it is set to false, the animation root tag is continuously played, android: Duration indicates the duration of the image to be displayed. each item is a frame, and drawable indicates the image to be displayed, duration is the display time, Android: oneshot = "false" indicates that the animation has been in progress, if it is true, it indicates that only one animation is played --> <animation-list xmlns: Android = "http://schemas.android.com/apk/res/android" Android: oneshot = "false"> <item Android: drawable = "@ drawable/number0" Android: Duration = "50"> </item> <item Android: drawable = "@ drawable/number1" Android: Duration = "50"> </item> <item Android: drawable = "@ drawable/number2" Android: duration = "50"> </item> <item Android: drawable = "@ drawable/Number3" Android: Duration = "50"> </item> <item Android: drawable = "@ drawable/number4" Android: Duration = "50"> </item> <item Android: drawable = "@ drawable/number5" Android: duration = "50"> </item> </animation-List>
Role_animation_drawable.xml
<? XML version = "1.0" encoding = "UTF-8"?> <! -- The root tag is animation-list. oneshot indicates whether to display the animation only once. If it is set to false, the animation root tag is continuously played, android: Duration indicates the duration of the image to be displayed. each item is a frame, and drawable indicates the image to be displayed, duration is the display time, Android: oneshot = "false" indicates that the animation has been in progress, if it is true, it indicates that only one animation is played --> <animation-list xmlns: Android = "http://schemas.android.com/apk/res/android" Android: oneshot = "false"> <item Android: drawable = "@ drawable/role1" Android: Duration = "60"> </item> <item Android: drawable = "@ drawable/role2" Android: Duration = "60"> </item> <item Android: drawable = "@ drawable/role3" Android: duration = "60"> </item> <item Android: drawable = "@ drawable/role4" Android: Duration = "60"> </item> </animation-List>
Http://download.csdn.net/detail/niejing654092427/3880651 Resources
Example 2:
Step 1: First slice the clips and put the following clips in the Res/drawable directory:
Image material: |
|
|
|
|
|
|
File Name: |
Wifi1.png |
Wif2.png |
Wifi3.png |
Wifi4.png |
Wifi5.png |
Wifi6.png
|
Step 2: There are two layout files in the animation-list. One is an animation displayed in order and the other is an animation displayed in reverse order. The files are stored in the Res/anim directory.
Sequence Display of animation files:Animation1.xml
<? XML version = "1.0" encoding = "UTF-8"?> <! -- The root tag is animation-list. oneshot indicates whether to display the animation only once. If it is set to false, the animation root tag is continuously played, android: Duration represents the length of time used to display each image in the animation through the item tag --> <animation-list xmlns: Android = "http://schemas.android.com/apk/res/android" Android: oneshot = "true"> <item Android: drawable = "@ drawable/wifi1" Android: Duration = "150"> </item> <item Android: drawable = "@ drawable/wifi2" Android: Duration = "150"> </item> <item Android: drawable = "@ drawable/wifi3" Android: duration = "150"> </item> <item Android: drawable = "@ drawable/wifi4" Android: Duration = "150"> </item> <item Android: drawable = "@ drawable/wifi5" Android: Duration = "150"> </item> <item Android: drawable = "@ drawable/wifi6" Android: duration = "150"> </item> </animation-List>
Display the animation file in reverse order: animation2.xml
<? XML version = "1.0" encoding = "UTF-8"?> <! -- The root tag is animation-list. oneshot indicates whether to display the animation only once. If it is set to false, the animation root tag is continuously played, android: Duration represents the length of time used to display each image in the animation through the item tag --> <animation-list xmlns: Android = "http://schemas.android.com/apk/res/android" Android: oneshot = "true"> <item Android: drawable = "@ drawable/wifi6" Android: Duration = "150"> </item> <item Android: drawable = "@ drawable/wifi5" Android: Duration = "150"> </item> <item Android: drawable = "@ drawable/wifi4" Android: duration = "150"> </item> <item Android: drawable = "@ drawable/wifi3" Android: Duration = "150"> </item> <item Android: drawable = "@ drawable/wifi2" Android: Duration = "150"> </item> <item Android: drawable = "@ drawable/wifi1" Android: duration = "150"> </item> </animation-List>
Step 3: layout the file in the Res/layout directory. The file name is activity_main.xml:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <imageview Android: id = "@ + ID/animationiv" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: padding = "5px" Android: src = "@ drawable/animation1"/> <button Android: Id = "@ + ID/one_btn" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: padding = "5px" Android: text = "sequence Display"/> <button Android: Id = "@ + ID/stop_btn" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: padding = "5px" Android: text = "stop"/> <button Android: Id = "@ + ID/two_btn" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: padding = "5px" Android: text = "reverse display"/> </linearlayout>
Step 4: Upload activity file, file name: mainactivity. Java
package com.example.animation;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity{ private ImageView animationIV; private Button buttonA, stopBtn, buttonC; private AnimationDrawable animationDrawable; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); animationIV = (ImageView) findViewById(R.id.animationIV); buttonA = (Button) findViewById(R.id.one_btn); stopBtn = (Button) findViewById(R.id.stop_btn); buttonC = (Button) findViewById(R.id.two_btn); buttonA.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub animationIV.setImageResource(R.drawable.animation1); animationDrawable = (AnimationDrawable) animationIV.getDrawable(); animationDrawable.start(); } }); stopBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub animationDrawable = (AnimationDrawable) animationIV.getDrawable(); animationDrawable.stop(); } }); buttonC.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub animationIV.setImageResource(R.drawable.animation2); animationDrawable = (AnimationDrawable) animationIV.getDrawable(); animationDrawable.start(); } }); }}
Resource:
Http://download.csdn.net/detail/niejing654092427/5540011