package com.example.examples_05_17;
import android.R.drawable;
import android.R.integer;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Toast;
public class GameView extends View {
//定义AnimationDrawable
public AnimationDrawable frameAnimation=null;
Context mContext;
//定义一个Drawable对象
Drawable mBitAnimation=null;
public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mContext=context;
//创建实例
frameAnimation=new AnimationDrawable();
//装载资源
/*
* 这里用一个for循环装载所有名字类型的资源
* 如"a1......a15.png"的图片
* 这个方法的用处很大
*/
for (int i = 1; i <= 15; i++) {
int id=getResources().getIdentifier("a"+i, "drawable", mContext.getPackageName());
//Toast.makeText(mContext, id+"", Toast.LENGTH_SHORT).show();
mBitAnimation=getResources().getDrawable(id);
//为动画添加一帧
//mBitAnimation该帧的图片
//参数500是该帧的显示时间,以毫秒算
frameAnimation.addFrame(mBitAnimation, 500);
}
//这里播放是否循环,true为循环,false为不循环
frameAnimation.setOneShot(true);
//设置本类将要显示这个动画
this.setBackgroundDrawable(frameAnimation);
}
}
package com.example.examples_05_17;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
public class MainActivity extends Activity {
GameView gameView=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView=new GameView(this);
setContentView(gameView);
}
public boolean onKeyDown(int keyCode,KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_DPAD_UP)
{
//开启播放
gameView.frameAnimation.start();
}
return true;
}
}
frame动画
时间: 2024-10-29 14:11:04