Frame Animate
語法:
<?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>
elements:
package com.msi.manning.chapter9.xmlanimate;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class XMLAnimate extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
ImageView img = (ImageView) findViewById(R.id.simple_anim);
img.setBackgroundResource(R.anim.simple_animation);
MyAnimationRoutine mar = new MyAnimationRoutine();
MyAnimationRoutine2 mar2 = new MyAnimationRoutine2();
Timer t = new Timer(false);
t.schedule(mar, 100);
Timer t2 = new Timer(false);
t2.schedule(mar2, 5000);
}
class MyAnimationRoutine extends TimerTask {
@Override
public void run() {
ImageView img = (ImageView) findViewById(R.id.simple_anim);
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
frameAnimation.start();
}
}
class MyAnimationRoutine2 extends TimerTask {
@Override
public void run() {
ImageView img = (ImageView) findViewById(R.id.simple_anim);
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
frameAnimation.stop();
}
}
}
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/simple_anim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, XMLAnimation" />
</LinearLayout>
simple_animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
id="selected"
android:oneshot="false" >
<item
android:drawable="@drawable/ball1"
android:duration="50"/>
<item
android:drawable="@drawable/ball2"
android:duration="50"/>
<item
android:drawable="@drawable/ball3"
android:duration="50"/>
<item
android:drawable="@drawable/ball4"
android:duration="50"/>
<item
android:drawable="@drawable/ball5"
android:duration="50"/>
<item
android:drawable="@drawable/ball6"
android:duration="50"/>
</animation-list>