import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.View;
/**
* @version 2012-8-21 下午03:01:24
**/
public class GameView extends View {
// 定义AnimationDrawable
private AnimationDrawable mAnimationDrawable = null;
// 定义一个Drawable
Drawable mDrawable = null;
Context context;
public GameView(Context context) {
super(context);
this.context = context;
// 实例化AnimationDrawable
mAnimationDrawable = new AnimationDrawable();
// 装载资源
for(int i = 1; i < 16; i++) {
int id = getResources().getIdentifier("a" + i, "drawable",
this.context.getPackageName());
mDrawable = getResources().getDrawable(id);
// 第一个参数是mDrawable显示的图片,第二个参数是显示时间 毫秒为单位
mAnimationDrawable.addFrame(mDrawable, 500);
}
// 是否循环 false 循环 true 不循环
mAnimationDrawable.setOneShot(false);
// 设置本类将要显示这个动画
this.setBackgroundDrawable(mAnimationDrawable);
setFocusable(true);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DPAD_UP) {
// 开始播放动画
mAnimationDrawable.start();
}
return super.onKeyDown(keyCode, event);
}
}
Frame动画java
最新推荐文章于 2022-01-19 01:15:09 发布