zoukankan      html  css  js  c++  java
  • 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状态
    	} 
    }
    

     

  • 相关阅读:
    SDN课程阅读作业(2)
    2019 SDN上机第4次作业
    第11组 Alpha事后诸葛亮
    第11组 Alpha冲刺(6/6)
    第11组 Alpha冲刺(5/6)
    2019 SDN上机第3次作业
    2019 SDN阅读作业
    第11组 Alpha冲刺(4/6)
    第11组 Alpha冲刺(3/6)
    模式识别和机器学习、数据挖掘的区别与联系(转发)
  • 原文地址:https://www.cnblogs.com/aikongmeng/p/3697395.html
Copyright © 2011-2022 走看看