Android游戏开发之使用AnimationDrable实现Frame动画

来源:互联网 发布:ar是什么软件 编辑:程序博客网 时间:2024/06/05 16:02

Android游戏开发之使用AnimationDrable实现Frame动画

Android开发中在制作2D帧动画中提供了使用XML配置动画文件的方式绘制,也就是说Android底层提供了动画播放的接口,那么我们分析一下如何调用它的接口来绘制动画。首先在工程res资源文件夹下创建anim动画文件夹,在这个文件夹中建立一个animation.xml文件, 这样它的路径就为re/anim/animation.xml。

看看内容应该是很好理解的,<animation-list>为动画的总标签,这里面放着帧动画 <item>标签,也就是说若干<item>标签的帧 组合在一起就是帧动画了。<animation-list > 标签中android:oneshot=”false” 这是一个非常重要的属性,默认为false 表示 动画循环播放, 如果这里写true 则表示动画只播发一次。 <item>标签中记录着每一帧的信息android:drawable=”@drawable/a”表示这一帧用的图片为”a”,下面以此类推。  android:duration=”100″ 表示这一帧持续100毫秒,可以根据这个值来调节动画播放的速度。

  1. <animation-list xmlns:android=”http://schemas.android.com/apk/res/android” android:oneshot=”false”>
  2.   <item android:drawable=”@drawable/a” android:duration=”100″ />
  3.   <item android:drawable=”@drawable/b” android:duration=”100″ />
  4.   <item android:drawable=”@drawable/c” android:duration=”100″ />
  5.   <item android:drawable=”@drawable/d” android:duration=”100″ />
  6.   <item android:drawable=”@drawable/e” android:duration=”100″ />
  7.   <item android:drawable=”@drawable/f” android:duration=”100″ />
  8.   <item android:drawable=”@drawable/g” android:duration=”100″ />
  9.   <item android:drawable=”@drawable/h” android:duration=”100″ />
  10.   <item android:drawable=”@drawable/i” android:duration=”100″ />
  11.   <item android:drawable=”@drawable/j” android:duration=”100″ />
  12.   </animation-list>

复制代码

下面这个例子的内容为 播放动画 与关闭动画 、设置播放类型 单次还是循环、拖动进度条修改动画的透明度,废话不多说直接进正题~~

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     android:orientation=”vertical”
  4.     android:layout_width=”fill_parent”
  5.     android:layout_height=”fill_parent”
  6.     >
  7. <LinearLayout
  8.     android:orientation=”horizontal”
  9.     android:layout_width=”wrap_content”
  10.     android:layout_height=”wrap_content”
  11.     >
  12. <Button
  13.    android:id=”@+id/button0″
  14.    android:layout_width=”wrap_content”
  15.    android:layout_height=”wrap_content”
  16.    android:text=”播放动画”
  17. />
  18.   <Button
  19.    android:id=”@+id/button1″
  20.    android:layout_width=”wrap_content”
  21.    android:layout_height=”wrap_content”
  22.    android:text=”停止动画”
  23. />
  24. </LinearLayout>
  25. <RadioGroup android:id=”@+id/radiogroup”
  26.             android:layout_width=”wrap_content”
  27.             android:layout_height=”wrap_content”
  28.            android:orientation=”horizontal”>
  29.    <RadioButton
  30.            android:id=”@+id/checkbox0″
  31.            android:layout_width=”wrap_content”
  32.            android:layout_height=”wrap_content”
  33.            android:checked=”true”
  34.            android:text=”单次播放”
  35.    />
  36.   <RadioButton
  37.            android:id=”@+id/checkbox1″
  38.            android:layout_width=”wrap_content”
  39.            android:layout_height=”wrap_content”
  40.            android:text=”循环播放”
  41.    />
  42.    </RadioGroup>
  43.     <TextView
  44.         android:layout_width=”wrap_content”
  45.         android:layout_height=”wrap_content”
  46.         android:text=”拖动进度条修改透明度(0 – 255)之间”
  47.         />
  48.   <SeekBar
  49.         android:id=”@+id/seekBar”
  50.         android:layout_width=”fill_parent”
  51.         android:layout_height=”wrap_content”
  52.         android:max=”256″
  53.         android:progress=”256″/>
  54.   <ImageView
  55.    android:id=”@+id/imageView”
  56.    android:background=”@anim/animation”
  57.    android:layout_width=”wrap_content”
  58.    android:layout_height=”wrap_content”
  59. />
  60. </LinearLayout>

复制代码

这是一个比较简单的布局文件,应该都能看懂吧。  我主要说一下 最后的这个 ImageView, 它就是用来显示我们的动画。 这里使用android:background=”@anim/animation”设置这个ImageView现实的背景为一个动画,动画资源的路径为res/anim/animation.xml   ,当然 设置background同样也可以在代码中设置。

  1.         imageView.setBackgroundResource(R.anim.animation);

复制代码

通过getBackground方法就可以拿到这个animationDrawable对象。

  1.         /**拿到ImageView对象**/
  2.         imageView = (ImageView)findViewById(R.id.imageView);
  3.         /**通过ImageView对象拿到背景显示的AnimationDrawable**/
  4.         animationDrawable = (AnimationDrawable) imageView.getBackground();

复制代码

AnimationDrawable 就是用来控制这个帧动画,这个类中提供了很多方法。

animationDrawable.start(); 开始这个动画
animationDrawable.stop(); 结束这个动画
animationDrawable.setAlpha(100);设置动画的透明度, 取值范围(0 – 255)
animationDrawable.setOneShot(true); 设置单次播放
animationDrawable.setOneShot(false); 设置循环播放
animationDrawable.isRunning(); 判断动画是否正在播放
animationDrawable.getNumberOfFrames(); 得到动画的帧数。

将这个例子的完整代码贴上

  1. import android.app.Activity;
  2. import android.graphics.drawable.AnimationDrawable;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. import android.widget.RadioButton;
  10. import android.widget.RadioGroup;
  11. import android.widget.SeekBar;
  12. import android.widget.SeekBar.OnSeekBarChangeListener;
  13. public class SimpleActivity extends Activity {
  14.     /**播放动画按钮**/
  15.     Button button0 = null;
  16.     /**停止动画按钮**/
  17.     Button button1 = null;
  18.     /**设置动画循环选择框**/
  19.     RadioButton radioButton0= null;
  20.     RadioButton radioButton1= null;
  21.     RadioGroup  radioGroup = null;
  22.     /**拖动图片修改Alpha值**/
  23.     SeekBar seekbar = null;
  24.     /**绘制动画View**/
  25.     ImageView imageView = null;
  26.     /**绘制动画对象**/
  27.     AnimationDrawable animationDrawable = null;
  28.     @Override
  29.     public void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.simple);
  32.         /**拿到ImageView对象**/
  33.         imageView = (ImageView)findViewById(R.id.imageView);
  34.         /**通过ImageView对象拿到背景显示的AnimationDrawable**/
  35.         animationDrawable = (AnimationDrawable) imageView.getBackground();
  36.         /**开始播放动画**/
  37.         button0 = (Button)findViewById(R.id.button0);
  38.         button0.setOnClickListener(new OnClickListener() {
  39.             @Override
  40.             public void onClick(View arg0) {
  41.                 /**播放动画**/
  42.                 if(!animationDrawable.isRunning()) {
  43.                     animationDrawable.start();
  44.                 }
  45.             }
  46.         });
  47.         /**停止播放动画**/
  48.         button1 = (Button)findViewById(R.id.button1);
  49.         button1.setOnClickListener(new OnClickListener() {
  50.             @Override
  51.             public void onClick(View arg0) {
  52.                 /**停止动画**/
  53.                 if(animationDrawable.isRunning()) {
  54.                     animationDrawable.stop();
  55.                 }
  56.             }
  57.         });
  58.         /**单次播放**/
  59.         radioButton0 = (RadioButton)findViewById(R.id.checkbox0);
  60.         /**循环播放**/
  61.         radioButton1 = (RadioButton)findViewById(R.id.checkbox1);
  62.         /**单选列表组**/
  63.         radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
  64.         radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  65.             @Override
  66.             public void onCheckedChanged(RadioGroup radioGroup, int checkID) {
  67.                 if(checkID == radioButton0.getId()) {
  68.                     //设置单次播放
  69.                     animationDrawable.setOneShot(true);
  70.                 }else if (checkID == radioButton1.getId()) {
  71.                     //设置循环播放
  72.                     animationDrawable.setOneShot(false);
  73.                 }
  74.                 //发生改变后让动画重新播放
  75.                 animationDrawable.stop();
  76.                 animationDrawable.start();
  77.             }
  78.         });
  79.         /**监听的进度条修改透明度**/
  80.         seekbar = (SeekBar)findViewById(R.id.seekBar);
  81.         seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  82.             @Override
  83.             public void onStopTrackingTouch(SeekBar seekBar) {
  84.             }
  85.             @Override
  86.             public void onStartTrackingTouch(SeekBar seekBar) {
  87.             }
  88.             @Override
  89.             public void onProgressChanged(SeekBar seekBar, int progress, boolean frameTouch) {
  90.                 /**设置动画Alpha值**/
  91.                 animationDrawable.setAlpha(progress);
  92.                 /**通知imageView 刷新屏幕**/
  93.                 imageView.postInvalidate();
  94.             }
  95.         });
  96.     }
  97. }

复制代码

拖动进度条设置Alpha值的时候 一定要使用     imageView.postInvalidate(); 方法来通知UI线程重绘屏幕中的imageView  否则会看不到透明的效果 。这里切记切记~~

3.gif (28.68 KB, 下载次数: 168)

下载附件  保存到相册

2011-9-6 20:31 上传

原创粉丝点击