Android canvast View 代码实例


package com.app.canvastest;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;

public class MapView extends View {

	Bitmap bitmap = null;
	int[][] map_data ={
			{ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
				7, 7, 7, 7, 7, 7, 1, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
				7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
				0, 0, 0, 0 },
				{ 7, 2, 2, 2, 2, 2, 2, 7, 2, 2, 2, 2, 2, 2, 2, 2, 
					2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
					2, 2, 2, 2, 2, 7, 2, 2, 2, 7, 2, 2, 2, 0, 0, 0, 
					0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
					0, 0, 0, 0 },
					{ 7, 2, 2, 2, 2, 2, 2, 7, 2, 2, 2, 2, 2, 2, 2, 2, 
						2, 2, 11, 2, 2, 2, 2, 8, 2, 2, 2, 2, 2, 6, 2, 2, 
						2, 2, 2, 2, 2, 7, 2, 2, 2, 7, 2, 21, 21, 2, 2, 0, 
						0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 
						0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
						0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
						0, 0, 0, 0 },
						{ 7, 2, 13, 13, 13, 13, 13, 7, 2, 2, 2, 2, 6, 2, 2, 8, 
							6, 2, 11, 2, 2, 2, 2, 2, 8, 2, 2, 2, 11, 6, 6, 2, 
							2, 2, 2, 2, 7, 7, 2, 2, 2, 7, 2, 2, 2, 2, 21, 21, 
							21, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 2, 
							2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
							0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
							0, 0, 0, 0 },
							{ 7, 2, 1, 2, 13, 2, 2, 7, 2, 2, 2, 2, 6, 2, 2, 8, 
								6, 2, 2, 11, 2, 2, 2, 2, 8, 2, 2, 11, 2, 2, 2, 6, 
								6, 2, 2, 2, 7, 2, 2, 2, 2, 7, 2, 2, 2, 2, 2, 2, 
								2, 2, 21, 21, 21, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
								2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
								0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
								0, 0, 0, 0 },
								{ 7, 2, 1, 1, 8, 2, 2, 7, 2, 2, 2, 6, 6, 2, 2, 8, 
									6, 2, 2, 11, 2, 2, 2, 2, 8, 2, 11, 2, 2, 2, 2, 2, 
									2, 2, 6, 2, 7, 2, 2, 2, 2, 7, 2, 2, 2, 2, 2, 2, 
									2, 2, 2, 2, 21, 21, 21, 21, 2, 2, 2, 2, 2, 2, 2, 2, 
									2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
									0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
									0, 0, 0, 0 }
	};

	public MapView(Context context) {
		super(context);
		setFocusable(true);
		try {
			bitmap = BitmapFactory.decodeResource(context.getResources(),
					R.drawable.map); 
		} catch (Exception e) {
			e.printStackTrace();
		} 
	} 
	protected void onDraw(Canvas canvas) {
		try {
			super.onDraw(canvas);
			Paint paint = new Paint(); 
			// 地图总行数
			int maxRows = this.map_data.length;
			// 地图总列数
			int maxCols = this.map_data[0].length;
			for (int row = 0; row < maxRows; row++) {
				for (int col = 0; col < maxCols; col++) {
					// 小图编号
					int imgId = this.map_data[row][col];
					imgId = imgId - 1;
					// 小图在手机屏幕上的y坐标
					int y = row * 24;
					// 小图在手机屏幕上的y坐标
					int x = col * 24;
					// 小图在大图上的行数
					int smallImageRow = imgId / 7;
					// 小图在大图上的偏移量
					int offsetY = smallImageRow * 24;
					int smallImagecol = imgId % 7;
					int offsetX = smallImagecol * 24;
					// 画大图中的小图
					drawClipImage(canvas, paint, this.bitmap, x, y, offsetX,
							offsetY, 24, 24);
					Log.d("game", x + "," + y + "," + offsetX + "," + offsetY);
				}
				Log.d("game", "-------------------------");
			}
			paint.setColor(0xFFFF0000);
			canvas.drawText("aa", 50, 300, paint);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void drawClipImage(Canvas canvas, Paint paint, Bitmap bitmap,
			int x, int y, int offsetX, int offsetY, int width, int height) {
		canvas.save();// 记录原来的canvas状态
		canvas.clipRect(x, y, x + width, y + height);
		canvas.drawBitmap(bitmap, x - offsetX, y - offsetY, paint);
		canvas.restore();// 恢复canvas状态
	} 
}

 

posted @ 2013-07-22 20:29  一个学渣  阅读(224)  评论(0编辑  收藏  举报

 

CSDNGitHub 联系邮箱:aikongmeng@gmail.com


Copyright 2008-2019 Government All Rights Reserved