1.java代码实现:

package com.Aina.Android;import android.content.Context;import android.graphics.Canvas;import android.graphics.drawable.AnimationDrawable;import android.graphics.drawable.Drawable;import android.view.KeyEvent;import android.view.View;import android.widget.ImageView;/** * com.Aina.Android Pro_AnimationFrame *  * @author Aina.huang E-mail: 674023920@qq.com * @version 创建时间:2010 Jun 18, 2010 1:56:18 PM 类说明 */public class GameView extends View {private AnimationDrawable mAnimationDrawable = null;private Context mContext = null;private Drawable mDrawable = null;public GameView(Context context) {super(context);this.mContext = context;mAnimationDrawable = new AnimationDrawable();for (int i = 1; i <= 15; i++) {int id = this.getResources().getIdentifier("a" + i, "drawable",mContext.getPackageName());mDrawable = this.getResources().getDrawable(id);mAnimationDrawable.addFrame(mDrawable, 100);// 为动画添加一帧.时间为毫秒}mAnimationDrawable.setOneShot(false);// 设置播放模式是否循环,false循环,true不循环.//ImageView iv = new ImageView(mContext);//iv.setBackgroundResource(R.anim.frame);//mAnimationDrawable = (AnimationDrawable) iv.getBackground();this.setBackgroundDrawable(mAnimationDrawable);// 显示动画.}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);}public boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode==KeyEvent.KEYCODE_1){mAnimationDrawable.start();//启动动画.}return super.onKeyDown(keyCode, event);}}


package com.Aina.Android;import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;public class Test_Frame extends Activity {    /** Called when the activity is first created. */private GameView gv = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        gv = new GameView(this);        setContentView(gv);    }@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {return gv.onKeyDown(keyCode, event);}    }


2.xml布局文件实现:

<?xml version="1.0" encoding="utf-8"?><animation-listxmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"><item android:drawable="@drawable/a1" android:duration="100" /><item android:drawable="@drawable/a2" android:duration="100" /><item android:drawable="@drawable/a3" android:duration="100" /><item android:drawable="@drawable/a4" android:duration="100" /><item android:drawable="@drawable/a5" android:duration="100" /><item android:drawable="@drawable/a6" android:duration="100" /><item android:drawable="@drawable/a7" android:duration="100" /><item android:drawable="@drawable/a8" android:duration="100" /><item android:drawable="@drawable/a9" android:duration="100" /><item android:drawable="@drawable/a10" android:duration="100" /><item android:drawable="@drawable/a11" android:duration="100" /><item android:drawable="@drawable/a12" android:duration="100" /><item android:drawable="@drawable/a13" android:duration="100" /><item android:drawable="@drawable/a14" android:duration="100" /><item android:drawable="@drawable/a15" android:duration="100" /></animation-list>


package com.Aina.Android;import android.content.Context;import android.graphics.Canvas;import android.graphics.drawable.AnimationDrawable;import android.graphics.drawable.Drawable;import android.view.KeyEvent;import android.view.View;import android.widget.ImageView;/** * com.Aina.Android Pro_AnimationFrame *  * @author Aina.huang E-mail: 674023920@qq.com * @version 创建时间:2010 Jun 18, 2010 1:56:18 PM 类说明 */public class GameView extends View {private AnimationDrawable mAnimationDrawable = null;private Context mContext = null;private Drawable mDrawable = null;public GameView(Context context) {super(context);this.mContext = context;/*mAnimationDrawable = new AnimationDrawable();for (int i = 1; i <= 15; i++) {int id = this.getResources().getIdentifier("a" + i, "drawable",mContext.getPackageName());mDrawable = this.getResources().getDrawable(id);mAnimationDrawable.addFrame(mDrawable, 100);// 为动画添加一帧.时间为毫秒}mAnimationDrawable.setOneShot(false);// 设置播放模式是否循环,false循环,true不循环.*/ImageView iv = new ImageView(mContext);iv.setBackgroundResource(R.anim.frame);mAnimationDrawable = (AnimationDrawable) iv.getBackground();this.setBackgroundDrawable(mAnimationDrawable);// 显示动画.}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);}public boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode==KeyEvent.KEYCODE_1){mAnimationDrawable.start();//启动动画.}return super.onKeyDown(keyCode, event);}}

更多相关文章

  1. Android中获取系统的时间
  2. 【Android】Vibrator(震动模式)设置长短震动
  3. Android 情景模式提示音默认修改
  4. android 显示系统时间
  5. android得到系统时间如何判断是白天还是晚上
  6. Android中帧动画实现
  7. android定制化开关机动画、铃音
  8. Android是否可以实现静默安装模式

随机推荐

  1. android 代码proguard
  2. android核心知识点学习资料
  3. android:属性 layout_alignParentRight an
  4. Android开发中修改程序字体
  5. Android用户界面 UI组件--自动提示输入框
  6. Android设备的唯一标识
  7. Android(安卓)图书总汇
  8. Android(安卓)Studio下载及离线升级方法
  9. Android中的5大布局和网格布局
  10. Android的控件属性