Before starting an instance, refer to a paragraph in the official documentation:
Frame animation is a series of pictures in a certain order to show the process, and the mechanism of the film is very similar to, we call frame-wise animation. Frame animations can be defined in an XML file or fully coded to be implemented.
If it is defined in an XML file, we can place it in the Anim or drawable directory under/res (/res/[anim | drawable]/filename.xml), the file name can be referenced in the code as a resource ID, and if it is implemented entirely by encoding, We need to use the Animationdrawable object.
If the animation is defined in an XML file, the syntax is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/res/ Android " android:oneshot=[" true "| "False" > <item android:drawable= "@[package:]drawable/drawable_resource_name" Android:duration= "integer"/></animation-list>
It is important to note that:
The <animation-list> element is required and must be a root element that can contain one or more <item> elements; android:onshot if defined as true, the animation is executed only once, If False, it is always looped.
The <item> element represents a frame animation, android:drawable specifies the picture resource corresponding to this frame animation, and android:druation represents the duration, integer, and millisecond of this frame.
The next example of the document is that I'm not going to explain it, because then we'll have to illustrate the process with our own examples.
We created a new project called Anim, named four consecutive images as f1.png,f2.png,f3.png,f4.png, placed in the drawable directory, and created a new Frame.xml file:
<?xml version= "1.0" encoding= "Utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/res/ Android " android:oneshot=" false "> <item android:drawable=" @drawable/f1 "android:duration= "/>" <item android:drawable= "@drawable/f2" android:duration= "" "/> <item android:drawable = "@drawable/f3" android:duration= "/> <item android:drawable=" @drawable/f4 "android:duration=" 300 "/ ></animation-list>
We can put the Frame.xml file in the drawable or Anim directory, the official document is placed in the drawable, people can be placed according to preferences, placed in both directories can be run.
Then introduce the layout file Res/layout/frame.xml:
<?xml version= "1.0" encoding= "Utf-8"?><LinearLayout xmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "Vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <ImageView Android:id= "@+id/frame_image"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:layout_weight= "1"/> <Button android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Stopframe"Android:onclick= "Stopframe"/> <Button android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Runframe"Android:onclick= "Runframe"/></linearlayout>
We defined a imageview as the vector for the animation, and then defined two buttons, namely stop and start animation.
Here's how to animate the animation by loading an animation definition file. We will first write:
PackageCom.scott.anim;Importandroid.app.Activity;Importandroid.graphics.drawable.AnimationDrawable;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.ImageView; Public classFrameactivityextendsActivity {PrivateImageView image; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.frame); Image=(ImageView) Findviewbyid (r.id.frame_image); Image.setbackgroundresource (R.anim.frame); Animationdrawable Anim=(animationdrawable) image.getbackground (); Anim.start (); }}
It seems perfect, as it is written on official documents, but when we run this program we find that it only stays in the first frame, and there is no animation that we expect, perhaps you will be disappointed to say "why?" and then you put the corresponding code in a button click event, the animation is executed smoothly, Move back to the OnCreate, still no effect, this time estimated you will be flustered roar a: "What the fuck!". But what is the reason? How to solve it?
This behavior occurs because when we call the Start method of animationdrawable in OnCreate, the Windows window object is not fully initialized, and animationdrawable cannot be fully appended to the Window object. So what should we do? We need to put this code in the Onwindowfocuschanged method, and when the activity is presented to the user, the Onwindowfocuschanged method is called, and that's when we animate it. Of course, Onwindowfocuschanged was called after OnCreate,
Then we need to rewrite the code:
PackageCom.scott.anim;Importandroid.app.Activity;Importandroid.graphics.drawable.AnimationDrawable;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.ImageView; Public classFrameactivityextendsActivity {PrivateImageView image; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.frame); Image=(ImageView) Findviewbyid (r.id.frame_image); } @Override Public voidOnwindowfocuschanged (BooleanHasfocus) { Super. onwindowfocuschanged (Hasfocus); Image.setbackgroundresource (R.anim.frame); Animationdrawable Anim=(animationdrawable) image.getbackground (); Anim.start (); }}
Run it and the animation will display properly.
If, in some cases, we need to implement an animation in a purely code way, we can write:
New animationdrawable (); for (int i = 1; I <= 4; i++) { int id = getresources (). Getidentifier ("F" + I, "drawable" , Getpackagename ()); = getresources (). getdrawable (ID); );} Anim.setoneshot (false); image.setbackgrounddrawable (Anim); Anim.start ();
The complete Frameactivity.java code is as follows:
PackageCom.scott.anim;Importandroid.app.Activity;Importandroid.graphics.drawable.AnimationDrawable;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.ImageView; Public classFrameactivityextendsActivity {PrivateImageView image; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.frame); Image=(ImageView) Findviewbyid (r.id.frame_image); } @Override Public voidOnwindowfocuschanged (BooleanHasfocus) { Super. onwindowfocuschanged (Hasfocus); Image.setbackgroundresource (R.anim.frame); //Set the animation resource file to the background of ImageViewAnimationdrawable Anim = (animationdrawable) image.getbackground ();//gets the ImageView background, which is now compiled into animationdrawableAnim.start ();//Start Animation } Public voidstopframe (view view) {animationdrawable Anim=(animationdrawable) image.getbackground (); if(Anim.isrunning ()) {//if it is running, stopAnim.stop (); } } Public voidrunframe (view view) {//full-code-implemented animation effectsAnimationdrawable Anim =Newanimationdrawable (); for(inti = 1; I <= 4; i++) { //gets the corresponding resource ID in the R.java based on the resource name and directory intid = getresources (). Getidentifier ("F" + I, "drawable", Getpackagename ()); //gets the Drawable object based on the resource IDDrawable drawable =getresources (). getdrawable (ID); //Add this frame to the animationdrawableAnim.addframe (drawable, 300); } anim.setoneshot (false);//set to LoopImage.setbackgrounddrawable (ANIM);//set the animation to ImageView backgroundAnim.start ();//Start Animation }}
Reprint Address: http://blog.csdn.net/liuhe688/article/details/6657776
Detailed Android Animation frame Animation (Turn)