Android Animation Frame动画
16lz
2021-01-23
1.java代码实现:
2.xml布局文件实现:
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);}}
更多相关文章
- Android中获取系统的时间
- 【Android】Vibrator(震动模式)设置长短震动
- Android 情景模式提示音默认修改
- android 显示系统时间
- android得到系统时间如何判断是白天还是晚上
- Android中帧动画实现
- android定制化开关机动画、铃音
- Android是否可以实现静默安装模式