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状态
}
}

最新文章

  1. sql server数据库连接问题处理
  2. DateTime的精度小问题
  3. Spring Collections XML 配置
  4. SpringMVC控制器与视图的数据交换
  5. Java中long和Long有什么区别 (转载)
  6. [js开源组件开发]图片放大镜
  7. 网页撤销后ubuntu本地撤销
  8. 【转】使用命令行方式创建和启动android模拟器
  9. c#中 uint--byte[]--char[]--string相互转换汇总
  10. sqoop 操作从hdfs 导入到mysql中语句
  11. Redis的使用初探
  12. Orientation Auto Rotation旋转屏幕crash问题(Unity3D开发之十四)
  13. 【git】git hello world
  14. UE4外包团队:更新一下UE4和Unity3D案例
  15. vue实例相关
  16. spring aop 获取request、response对象
  17. jQuery样式--css(name|pro|[,val|fn])
  18. 同一台电脑使用多个ssh连接git,出现权限不够的问题
  19. python标准库--functools.partial
  20. 用AR.js实现webAR(新手入门)

热门文章

  1. flask多app和栈的应用
  2. 51Nod 1668 非010串
  3. [Oracle]发生 ora-06502 RMAN 在对 catalog DB 同期时出错的调查方法
  4. B. Forgery
  5. Week 3 结对编程
  6. idea 设置注释
  7. android计算器
  8. 小学四则运算APP 第一阶段冲刺
  9. 第三个spring冲刺总结(附团队贡献分)
  10. Python轻松爬取Rosimm写真网站全部图片