package com.android.game;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class Activity01 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new MapView(this));
}
public class MapView extends View{
//tile块的宽高
public final static int TILE_WIDTH =32;
public final static int TILE_HEIGHT = 32;
//tile块的宽高数量
public final static int TILE_WIDTH_COUNT=10;
public final static int TILE_HEIGHT_COUNT = 15;
//数组元素为0什么都不画
public final static int TILE_NULL = 0;
//第一层 游戏VIew地图数组
public int [][] mMapView={
{ 1, 1, 1, 1, 137, 137, 137, 1, 1, 1 },
{ 1, 1, 1, 1, 137, 137, 137, 1, 1, 1 },
{ 1, 1, 1, 1, 137, 137, 137, 1, 1, 1 },
{ 137, 137, 137, 137, 137, 137, 137, 137, 137, 137 },
{ 137, 137, 137, 137, 137, 137, 137, 137, 137, 137 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 137, 137 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 137, 137 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 137, 137 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 137, 137 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 137, 137 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 137, 137 },
{ 137, 137, 137, 137, 137, 137, 137, 137, 137, 137 },
{ 137, 137, 137, 137, 137, 137, 137, 137, 137, 137 },
{ 1, 1, 1, 1, 1, 137, 137, 137, 1, 1 },
{ 1, 1, 1, 1, 1, 137, 137, 137, 1, 1 }
};
//第二层 游戏实体actor数组
public int [][] mMapAcotor ={
{ 102, 103, 103, 104, 0, 0, 0, 194, 195, 196 },
{ 110, 111, 111, 112, 0, 0, 0, 202, 203, 204 },
{ 118, 119, 119, 120, 0, 0, 0, 210, 211, 212 },
{ 126, 127, 127, 128, 0, 0, 0, 218, 219, 220 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 2, 2, 2, 2, 2, 2, 2, 11, 0, 0 },
{ 233, 234, 235, 236, 14, 14, 14, 14, 0, 0 },
{ 241, 242, 243, 244, 148, 148, 148, 148, 0, 0 },
{ 249, 250, 251, 252, 148, 148, 148, 148, 0, 0 },
{ 0, 258, 259, 0, 0, 0, 0, 11, 0, 0 },
{ 6, 6, 6, 6, 6, 6, 6, 6, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 102, 103, 103, 104, 0, 0, 0, 0, 143, 144 },
{ 110, 111, 111, 112, 0, 0, 0, 0, 151, 152 }
};
//游戏地图资源
Bitmap mBitmap = null;
//资源文件
Resources mResources = null;
//游戏画笔
Paint mPaint = null;
//横向纵向Title块的数量
int mWidthTileCount = 0;
int mHeightTileCount = 0;
//横向纵向tile块的数量
int mBitMapWidth = 0;
int mBitMapHeight =0;
public MapView(Context context){
super(context);
mPaint = new Paint();
mBitmap = ReadBitMap(context,R.drawable.game);
mBitMapWidth = mBitmap.getWidth();
mBitMapHeight = mBitmap.getHeight();
mWidthTileCount = mBitMapWidth/TILE_WIDTH;
mHeightTileCount = mBitMapHeight/TILE_HEIGHT;
}
protected void onDraw(Canvas canvas){
DrawMap(canvas,mPaint,mBitmap);
super.onDraw(canvas);
}
private void DrawMap(Canvas canvas,Paint paint ,Bitmap bitmap){
int i,j;
for(i = 0;i<TILE_HEIGHT_COUNT;i++){
for(j = 0;j<TILE_WIDTH_COUNT;j++){
int ViewID = mMapView[i][j];
int ActorID = mMapAcotor[i][j];
//绘制地图的第一层
if(ViewID>TILE_NULL){
DrawMapTile(ViewID,canvas,paint,bitmap,j*TILE_WIDTH,i*TILE_HEIGHT
);
}
//绘制地图第二层
if(ActorID>TILE_NULL){
DrawMapTile(ActorID,canvas,paint,bitmap,j*TILE_WIDTH,i*TILE_HEIGHT);
}
}
}
}
private void DrawMapTile(int id,Canvas canvas,Paint paint,Bitmap bitmap,int x,int y){
id--;
int count = id/mWidthTileCount;
int bitmapX =(id-(count*mWidthTileCount))*TILE_WIDTH;
int bitmapY = count*TILE_HEIGHT;
DrawClipImage(canvas,paint,bitmap,x,y,bitmapX,bitmapY,TILE_WIDTH,TILE_HEIGHT);
}
public Bitmap ReadBitMap(Context context,int resId){
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}
private void DrawClipImage(Canvas canvas,Paint paint,Bitmap bitmap,int x,int y, int src_x,int src_y,
int src_px,int src_py){
canvas.save();
canvas.clipRect(x, y, x+src_px,y+src_py);
canvas.drawBitmap(bitmap,x-src_x,y-src_y,paint);
canvas.restore();
}
}
}
这个模式是向雨松MOMO学习的,更多请参考他的CSDN博客http://blog.csdn.net/xys289187120
全站熱搜
留言列表