【Android】Android圖形之Animate

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:
<animation-list>
Required. This must be the root element. Contains one or more <item> elements.

attributes:

android:oneshot
Boolean. "true" if you want to perform the animation once; "false" to loop the animation.
<item>
A single frame of animation. Must be a child of a <animation-list> element.

attributes:

android:drawable
Drawable resource. The drawable to use for this frame.
android:duration

Integer. The duration to show this frame, in milliseconds.


<?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/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);

rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();


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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章