有时需要在程序中播放一段帧动画,例如自己做了几张图片,想作为动画播放起来,可以借助android.graphics.drawable.AnimationDrawable类来帮助实现。
具体方法如下:
步骤一:先在res目录下(或anim目录)创建一个XML,里面保存每个图片的信息
animation-list标签就代表,这是一组动画的列表
android:oneshot属性,表示循环播放,true则只播放一次,false表示循环一直播放。
item标签代表各个帧元素
android:duration属性代表帧与帧之间的持续时间,以毫秒为单位
android:drawable属性代表具体的图片信息。
步骤二:保存完动画的列表,就可以创建一个界面布局来进行设计了
/res/layout/main.xml
按钮和其他布局没有什么好说的,主要说下ImageView控件。
android:background属性直接设置成了刚才定义的那个动画列表,通常都是把动画定义为ImageView的background属性。
步骤三:创建完布局,就可以再代码里控制了。
AnimationTestMain.java
具体方法如下:
步骤一:先在res目录下(或anim目录)创建一个XML,里面保存每个图片的信息
/res/anim/loading.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list
android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:duration="150" android:drawable="@drawable/recording1" />
<item android:duration="150" android:drawable="@drawable/recording2" />
<item android:duration="150" android:drawable="@drawable/recording3" />
<item android:duration="150" android:drawable="@drawable/recording4" />
<item android:duration="150" android:drawable="@drawable/recording5" />
<item android:duration="150" android:drawable="@drawable/recording6" />
<item android:duration="150" android:drawable="@drawable/recording7" />
</animation-list>animation-list标签就代表,这是一组动画的列表
android:oneshot属性,表示循环播放,true则只播放一次,false表示循环一直播放。
item标签代表各个帧元素
android:duration属性代表帧与帧之间的持续时间,以毫秒为单位
android:drawable属性代表具体的图片信息。
步骤二:保存完动画的列表,就可以创建一个界面布局来进行设计了
/res/layout/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" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="start"
/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="end"
/>
</LinearLayout>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:background="@anim/loading"
/>
</LinearLayout>按钮和其他布局没有什么好说的,主要说下ImageView控件。
android:background属性直接设置成了刚才定义的那个动画列表,通常都是把动画定义为ImageView的background属性。
步骤三:创建完布局,就可以再代码里控制了。
AnimationTestMain.java
package com.carter.animationtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.graphics.drawable.AnimationDrawable;
public class AnimationTestMain extends Activity{
Button btnStart, btnStop;
ImageView mImage;
AnimationDrawable anim;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStart = (Button)findViewById(R.id.bt1);
btnStop = (Button)findViewById(R.id.bt2);
mImage = (ImageView)findViewById(R.id.image);
anim = (AnimationDrawable)mImage.getBackground();
btnStart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
anim.start();
}
});
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
anim.stop();
}
});
}
}定义一个AnimationDrawable对象,通过ImageView的getBackground()方法来强制类型转换为一个AnimationDrawable。
这样就可已通过start()和stop()方法来进行控制动画播放了。
本文介绍如何利用Android的AnimationDrawable类实现帧动画播放。通过创建XML文件定义动画列表及属性,结合ImageView组件和背景属性,实现动画的启动与停止控制。

2574

被折叠的 条评论
为什么被折叠?



