public class GameView extends SurfaceView implements
SurfaceHolder.Callback {
//屏幕宽高
public static int SCREEN_WIDTH;
public static int SCREEN_HEIGHT;
private Context mContext;
private SurfaceHolder mHolder;
//最大帧数 (1000 / 30)
private static final int DRAW_INTERVAL = 30;
private DrawThread mDrawThread;
private FrameAnimation []spriteAnimations;
private Sprite mSprite;
private int spriteWidth = 0;
private int spriteHeight = 0;
private float spriteSpeed = (float)((500 * SCREEN_WIDTH / 480) * 0.001);
private int row = 4;
private int col = 4;
public GameSurfaceView(Context context) {
super(context);
this.mContext = context;
mHolder = this.getHolder();
mHolder.addCallback(this);
initResources();
mSprite = new Sprite(spriteAnimations,0,0,spriteWidth,spriteHeight,spriteSpeed);
}
private void initResources() {
Bitmap[][] spriteImgs = generateBitmapArray(mContext, R.drawable.sprite, row, col);
spriteAnimations = new FrameAnimation[row];
for(int i = 0; i < row; i ++) {
Bitmap []spriteImg = spriteImgs[i];
FrameAnimation spriteAnimation = new FrameAnimation(spriteImg,new int[]{150,150,150,150},true);
spriteAnimations[i] = spriteAnimation;
}
}
public Bitmap decodeBitmapFromRes(Context context, int resourseId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
InputStream is = context.getResources().openRawResource(resourseId);
return BitmapFactory.decodeStream(is, null, opt);
}
public Bitmap createBitmap(Context context, Bitmap source, int row,
int col, int rowTotal, int colTotal) {
Bitmap bitmap = Bitmap.createBitmap(source,
(col - 1) * source.getWidth() / colTotal,
(row - 1) * source.getHeight() / rowTotal, source.getWidth()
/ colTotal, source.getHeight() / rowTotal);
return bitmap;
}
public Bitmap[][] generateBitmapArray(Context context, int resourseId,
int row, int col) {
Bitmap bitmaps[][] = new Bitmap[row][col];
Bitmap source = decodeBitmapFromRes(context, resourseId);
this.spriteWidth = source.getWidth() / col;
this.spriteHeight = source.getHeight() / row;
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; j++) {
bitmaps[i - 1][j - 1] = createBitmap(context, source, i, j,
row, col);
}
}
if (source != null && !source.isRecycled()) {
source.recycle();
source = null;
}
return bitmaps;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
if(null == mDrawThread) {
mDrawThread = new DrawThread();
mDrawThread.start();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
if(null != mDrawThread) {
mDrawThread.stopThread();
}
}
private class DrawThread extends Thread {
public boolean isRunning = false;
public DrawThread() {
isRunning = true;
}
public void stopThread() {
isRunning = false;
boolean workIsNotFinish = true;
while (workIsNotFinish) {
try {
this.join();// 保证run方法执行完毕
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
workIsNotFinish = false;
}
}
public void run() {
long deltaTime = 0;
long tickTime = 0;
tickTime = System.currentTimeMillis();
while (isRunning) {
Canvas canvas = null;
try {
synchronized (mHolder) {
canvas = mHolder.lockCanvas();
//设置方向
mSprite.setDirection();
//更新精灵位置
mSprite.updatePosition(deltaTime);
drawSprite(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != mHolder) {
mHolder.unlockCanvasAndPost(canvas);
}
}
deltaTime = System.currentTimeMillis() - tickTime;
if(deltaTime < DRAW_INTERVAL) {
try {
Thread.sleep(DRAW_INTERVAL - deltaTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
tickTime = System.currentTimeMillis();
}
}
}
private void drawSprite(Canvas canvas) {
//清屏操作
canvas.drawColor(Color.BLACK);
mSprite.draw(canvas);
}
}
public class Sprite {
public static final int DOWN = 0;
public static final int LEFT = 1;
public static final int RIGHT = 2;
public static final int UP = 3;
public float x;
public float y;
public int width;
public int height;
//精灵行走速度
public double speed;
//精灵当前行走方向
public int direction;
//精灵四个方向的动画
public FrameAnimation[] frameAnimations;
public Sprite(FrameAnimation[] frameAnimations, int positionX,
int positionY, int width, int height, float speed) {
this.frameAnimations = frameAnimations;
this.x = positionX;
this.y = positionY;
this.width = width;
this.height = height;
this.speed = speed;
}
public void updatePosition(long deltaTime) {
switch (direction) {
case LEFT:
//让物体的移动速度不受机器性能的影响,每帧精灵需要移动的距离为:移动速度*时间间隔
this.x = this.x - (float) (this.speed * deltaTime);
break;
case DOWN:
this.y = this.y + (float) (this.speed * deltaTime);
break;
case RIGHT:
this.x = this.x + (float) (this.speed * deltaTime);
break;
case UP:
this.y = this.y - (float) (this.speed * deltaTime);
break;
}
}
/**
* 根据精灵的当前位置判断是否改变行走方向
*/
public void setDirection() {
if (this.x <= 0
&& (this.y + this.height) < GameSurfaceView.SCREEN_HEIGHT) {
if (this.x < 0)
this.x = 0;
this.direction = Sprite.DOWN;
} else if ((this.y + this.height) >= GameSurfaceView.SCREEN_HEIGHT
&& (this.x + this.width) < GameSurfaceView.SCREEN_WIDTH) {
if ((this.y + this.height) > GameSurfaceView.SCREEN_HEIGHT)
this.y = GameSurfaceView.SCREEN_HEIGHT - this.height;
this.direction = Sprite.RIGHT;
} else if ((this.x + this.width) >= GameSurfaceView.SCREEN_WIDTH
&& this.y > 0) {
if ((this.x + this.width) > GameSurfaceView.SCREEN_WIDTH)
this.x = GameSurfaceView.SCREEN_WIDTH - this.width;
this.direction = Sprite.UP;
} else {
if (this.y < 0)
this.y = 0;
this.direction = Sprite.LEFT;
}
}
public void draw(Canvas canvas) {
FrameAnimation frameAnimation = frameAnimations[this.direction];
Bitmap bitmap = frameAnimation.nextFrame();
if (null != bitmap) {
canvas.drawBitmap(bitmap, x, y, null);
}
}
}
public class FrameAnimation{
/**动画显示的需要的资源 */
private Bitmap[] bitmaps;
/**动画每帧显示的时间 */
private int[] duration;
/**动画上一帧显示的时间 */
protected Long lastBitmapTime;
/**动画显示的索引值,防止数组越界 */
protected int step;
/**动画是否重复播放 */
protected boolean repeat;
/**动画重复播放的次数*/
protected int repeatCount;
/**
* @param bitmap:显示的图片<br/>
* @param duration:图片显示的时间<br/>
* @param repeat:是否重复动画过程<br/>
*/
public FrameAnimation(Bitmap[] bitmaps, int duration[], boolean repeat) {
this.bitmaps = bitmaps;
this.duration = duration;
this.repeat = repeat;
lastBitmapTime = null;
step = 0;
}
public Bitmap nextFrame() {
// 判断step是否越界
if (step >= bitmaps.length) {
//如果不无限循环
if( !repeat ) {
return null;
} else {
lastBitmapTime = null;
}
}
if (null == lastBitmapTime) {
// 第一次执行
lastBitmapTime = System.currentTimeMillis();
return bitmaps[step = 0];
}
// 第X次执行
long nowTime = System.currentTimeMillis();
if (nowTime - lastBitmapTime <= duration[step]) {
// 如果还在duration的时间段内,则继续返回当前Bitmap
// 如果duration的值小于0,则表明永远不失效,一般用于背景
return bitmaps[step];
}
lastBitmapTime = nowTime;
return bitmaps[step++];// 返回下一Bitmap
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
DisplayMetrics outMetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
GameSurfaceView.SCREEN_WIDTH = outMetrics.widthPixels;
GameSurfaceView.SCREEN_HEIGHT = outMetrics.heightPixels;
GameSurfaceView gameView = new GameSurfaceView(this);
setContentView(gameView);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有