欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android frame by frame animation动画显示

程序员文章站 2022-03-16 15:09:53
...
在看到编写简单的动画的时候,想到了android上也可以做到这一点,只是几个图片来回的切换。这种显示方式学名叫做:frame by frame animation,顺序播放事先做好的图像,跟电影类似;

效果:
[img]http://dl.iteye.com/upload/attachment/526237/1d1a7439-eb83-37a0-b2d8-a6d04967e3fe.jpg[/img]

res/anim/picture_animation.xml:

<?xml version="1.0" encoding="utf-8"?>

<!-- 动画帧集合对象 -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<!--动画帧对象 android:duration表示每帧动画显示的时间,放在drawable下的动画图片不能太大,否则会内存爆掉 -->
<item android:drawable="@drawable/camp_fire1" android:duration="83" />
<item android:drawable="@drawable/camp_fire2" android:duration="83" />
<item android:drawable="@drawable/camp_fire3" android:duration="83" />
<item android:drawable="@drawable/camp_fire4" android:duration="83" />
<item android:drawable="@drawable/camp_fire5" android:duration="83" />
<item android:drawable="@drawable/camp_fire6" android:duration="83" />
<item android:drawable="@drawable/camp_fire7" android:duration="83" />
<item android:drawable="@drawable/camp_fire8" android:duration="83" />
<item android:drawable="@drawable/camp_fire9" android:duration="83" />
<item android:drawable="@drawable/camp_fire10" android:duration="83" />
<item android:drawable="@drawable/camp_fire11" android:duration="83" />
<item android:drawable="@drawable/camp_fire12" android:duration="83" />
<item android:drawable="@drawable/camp_fire13" android:duration="83" />
<item android:drawable="@drawable/camp_fire14" android:duration="83" />
<item android:drawable="@drawable/camp_fire15" android:duration="83" />
<item android:drawable="@drawable/camp_fire16" android:duration="83" />
<item android:drawable="@drawable/camp_fire17" android:duration="83" />
</animation-list>


main.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">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="start" android:id="@+id/start" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/imageId"
android:src="@anim/picture_animation" />
</LinearLayout>


IaiaiActivity.java类:

package com.iaiai.activity;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

/**
*
* <p>
* Title: IaiaiActivity.java
* </p>
* <p>
* E-Mail: [email protected]
* </p>
* <p>
* QQ: 176291935
* </p>
* <p>
* Http: iaiai.iteye.com
* </p>
* <p>
* Create time: 2011-7-28
* </p>
*
* @author 丸子
* @version 0.0.1
*/
public class IaiaiActivity extends Activity {

private ImageView imageView;

private AnimationDrawable draw = null;
private Button start;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView) findViewById(R.id.imageId);
this.draw = (AnimationDrawable) imageView.getDrawable();

this.start = (Button) findViewById(R.id.start);
this.start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (draw.isRunning()) {
draw.stop();
start.setText("start");
} else {
draw.stop();
draw.start();
start.setText("passe");
}
}
});

}
}


[color=darkred]注:代码运行的结果想必大家应该就知道了(图片按照顺序的播放一次),不过有一点需要强调的是:启动Frame Animation动画的代码rocketAnimation.start(); 不能在OnCreate()中,因为在OnCreate()中AnimationDrawable还没有完全的与ImageView绑定,在OnCreate()中启动动画,就只能看到第一张图片。[/color]

XML属性:
[table]
|属性|说明|
|drawable|当前帧引用的drawable资源|
|duration|当前帧显示的时间(毫秒为单位)|
|oneshot|如果为true,表示动画只播放一次停止在最后一帧上,如果设置为false表示动画循环播放。|
|variablePadding|If true, allows the drawable’s padding to change based on the current state that is selected.|
|visible|规定drawable的初始可见性,默认为flase;|
[/table]

下面,阅读Android SDK中对AnimationDrawable的介绍,有个简单的了解:
[table]
|AnimationDrawable||
|获取、设置动画的属性||
|int getDuration()|获取动画的时长|
|int getNumberOfFrames()|获取动画的帧数|
|boolean isOneShot()/Void setOneShot(boolean oneshot)|获取oneshot属性/设置oneshot属性|
|void inflate(Resurce r,XmlPullParser p,AttributeSet attrs)||
|增加、获取帧动画||
|Drawable getFrame(int index)|获取某帧的Drawable资源|
|void addFrame(Drawable frame,int duration)|为当前动画增加帧(资源,持续时长)|
|动画控制||
|void start()|开始动画|
|void run()|外界不能直接掉调用,使用start()替代|
|boolean isRunning()|当前动画是否在运行|
|void stop()|停止当前动画|
[/table]