【Android】Android图形之Animate

来源:互联网 发布:计算复杂性 口头算法 编辑:程序博客网 时间:2024/06/11 05:23

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 {@Overridepublic 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 {@Overridepublic void run() {ImageView img = (ImageView) findViewById(R.id.simple_anim);AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();frameAnimation.start();}}class MyAnimationRoutine2 extends TimerTask {@Overridepublic 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>