【Android】Android图形之Animate

本文介绍如何使用XML定义帧动画,通过示例代码展示了动画列表的结构及其属性,包括单帧动画的绘制资源和显示时长,并提供了启动和停止动画的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值