Before starting the example explanation, let's quote a passage from the official documentation:
Frame animation is a process in which a series of pictures are displayed in a certain order, which is very similar to the mechanism of playing a movie. We call it frame-by-frame animation. Frame animations can be defined in XML files or fully encoded.
If defined in an XML file, we can place it in the anim or drawable directory under /res (/res/[anim | drawable]/filename.xml), and the filename can be referenced in the code as a resource ID; Implemented by coding, 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>
have to be aware of is:
The <animation-list> element is required and must be used as the root element, which can contain one or more <item> elements; Android :onshot If defined as true, the animation will only be executed once, and if it is false, it will keep looping .
The <item> element represents a frame of animation, android:drawable specifies the image resource corresponding to this frame of animation, and android:druation represents the duration of this frame, an integer, in milliseconds.
I will not explain the following examples in the document, because we will also demonstrate this process with our own examples.
We create a new project named anim, name the four consecutive pictures as f1.png, f2.png, f3.png, f4.png, put them in the drawable directory, and then create 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="300" />
- <item android:drawable="@drawable/f2" android:duration="300" />
- <item android:drawable="@drawable/f3" android:duration="300" />
- <item android:drawable="@drawable/f4" android:duration="300" />
- </animation-list>
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>
Next, we will introduce how to achieve the effect of animation by loading the animation definition file. We would first write this:
- package com.scott.anim;
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ImageView;
- public class FrameActivity extends Activity {
- private ImageView image;
- @Override
- protected void onCreate(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();
- }
- }
This phenomenon occurs because when we call the start method of AnimationDrawable in onCreate, the window Window object has not been fully initialized, and the AnimationDrawable cannot be completely appended to the window Window object, so what should we do? We need to put this code in the onWindowFocusChanged method. When the Activity is displayed to the user, the onWindowFocusChanged method will be called, and it is at this time that we implement our animation effect. Of course, onWindowFocusChanged is called after onCreate, as shown in the figure:
Then we need to rewrite the code:
- package com.scott.anim;
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ImageView;
- public class FrameActivity extends Activity {
- private ImageView image;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate (savedInstanceState);
- setContentView(R.layout.frame);
- image = (ImageView) findViewById(R.id.frame_image);
- }
- @Override
- public void onWindowFocusChanged(boolean hasFocus) {
- super.onWindowFocusChanged(hasFocus);
- image.setBackgroundResource(R.anim.frame);
- AnimationDrawable anim = (AnimationDrawable) image.getBackground();
- anim.start();
- }
- }
If in some cases, we need to implement an animation in pure code, we can write:
- AnimationDrawable anim = new AnimationDrawable();
- for (int i = 1; i <= 4; i++) {
- int id = getResources().getIdentifier("f" + i, "drawable", getPackageName());
- Drawable drawable = getResources().getDrawable(id);
- anim.addFrame(drawable, 300);
- }
- anim.setOneShot(false);
- image.setBackgroundDrawable(anim);
- anim.start();
- package com.scott.anim;
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ImageView;
- public class FrameActivity extends Activity {
- private ImageView image;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate (savedInstanceState);
- setContentView(R.layout.frame);
- image = (ImageView) findViewById(R.id.frame_image);
- }
- @Override
- public void onWindowFocusChanged(boolean hasFocus) {
- super.onWindowFocusChanged(hasFocus);
- image.setBackgroundResource(R.anim.frame); //Set the animation resource file as the background of ImageView
- AnimationDrawable anim = (AnimationDrawable) image.getBackground(); //Get the ImageView background, which has been compiled into AnimationDrawable
- anim.start(); //Start animation
- }
- public void stopFrame(View view) {
- AnimationDrawable anim = (AnimationDrawable) image.getBackground();
- if (anim.isRunning()) { //If running, stop
- anim.stop();
- }
- }
- public void runFrame(View view) {
- // fully coded animation effect
- AnimationDrawable anim = new AnimationDrawable();
- for (int i = 1; i <= 4; i++) {
- //Get the corresponding resource ID in R.java according to the resource name and directory
- int id = getResources().getIdentifier("f" + i, "drawable", getPackageName());
- //Get the Drawable object according to the resource ID
- Drawable drawable = getResources().getDrawable(id);
- //Add this frame to AnimationDrawable
- anim.addFrame(drawable, 300);
- }
- anim.setOneShot( false ); //set to loop
- image.setBackgroundDrawable(anim); //Set the animation to the ImageView background
- anim.start(); //Start animation
- }
- }
Okay, here first, thank you all.
Original URL: http://blog.csdn.net/liuhe688/article/details/6657776