`

背景地图的构建(1)

阅读更多

1.地图的构建

首先,你要自己找到一张地图作为自己的游戏背景图,图片可能并不适用于所有

机型,但我们可以把它转化成Bitmap再改变其大小,让它适应手机屏幕。

       1.创建一个Bitmap对象,其实它就相当于一张画纸,把图片画上去.

       2.Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture) ;//这样就得到了和这张图片同意大小的画纸

       3.获得手机屏幕的大小

      

public  int[] getDeviceInfo(Context context) {
		if ((deviceWidthHeight[0] == 0) && (deviceWidthHeight[1] == 0)) {
			DisplayMetrics metrics = new DisplayMetrics();
			((Activity) context).getWindowManager().getDefaultDisplay()
					.getMetrics(metrics);

			deviceWidthHeight[0] = metrics.widthPixels;
			deviceWidthHeight[1] = metrics.heightPixels;
		}
		return deviceWidthHeight;
	}

             要得到屏幕的大小,在Activity初始化时取得:

    

int width = getDeviceInfo(this)[0] ;//返回宽度
int height = getDeviceInfo(this)[1] ;//返回长度

         4.创建一张你需要大小的Bitmap,利用矩阵映射把图像画上去

 

                                                                       

public static Bitmap resizeBitmap(Bitmap bitmap, int w, int h) {
		if (bitmap != null) {
			int width = bitmap.getWidth();
			int height = bitmap.getHeight();
			int newWidth = w;
			int newHeight = h;
			float scaleWidth = ((float) newWidth) / width;
			float scaleHeight = ((float) newHeight) / height;
			Matrix matrix = new Matrix();//矩阵映射
			matrix.postScale(scaleWidth, scaleHeight);
			Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
					height, matrix, true);
			return resizedBitmap;
		} else {
			return null;
		}
	}

               5.得到Canvas画布,利用Canvas.drawBitmap方法把图片画上去,这样就能得到合适的图片了             

 

 

 

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics