手游《奔跑吧?骚年!》技术分享(二):图片适配

做Android的游戏所面对的第一个问题就是图片适配,Android手机的分辨率的混乱程度我就不多说了。我在游戏里用个简单粗暴的解决方法:先预定义一个通用分辨率(在我的游戏用的是800X480 ),然后根据手机实际的分辨率来按比例改变图片的大小。这种方法有N多问题比如图片占用的内存过大、也会导致清晰度下降、不同的屏幕比例会导致图片有拉伸的情况等。但是目前我还没有发现有什么方法可以代替上述的方案,所以只能继续用了。。。

好了上代码:

package com.baohanbin.run_boy_run.game_util;


import android.graphics.Bitmap;
import android.graphics.Matrix;
/**
 * 屏幕适配器<br>
 * 修改图片大小以适配当前手机屏幕的分辨率<br>
 * 标准分辨率为800X480<br>
 * @author 包汉彬同学
 *
 */
public class ScreenAdapters {
//	标准分辨率的垂直像素
	public final static int HEIGHT = 480;
//	标准分辨率的水平像素
	public final static int WIDTH = 800;
//	当前手机的垂直像素
	public static int ScreenHeight;
//	当前手机的水平像素
	public static int ScreenWidth;
	/**
	 * 将图片按照给定的大小,适配当前屏幕
	 * @param bitmap 原图
	 * @param Width 标准分辨率的宽度
	 * @param Height标准分辨率的高度
	 * @return 适配后的图片
	 */
	public final static Bitmap AdaptersBitmap(Bitmap bitmap, float Width,
			float Height) {
		return AdaptersBitmap(bitmap, ScreenWidth, ScreenHeight, Width, Height);
	}
/**
 * 将图片按照给定的屏幕分辨率,进行适配
 * @param bitmap 原图
 * @param screenWidth 期待适配的分辨率的宽度
 * @param screenHeight 期待适配的分辨率的高度
 * @param Width 标准分辨率的宽度
 * @param Height 标准分辨率的高度
 * @return 适配后的图片
 */
	public final static Bitmap AdaptersBitmap(Bitmap bitmap, int screenWidth,
			int screenHeight, float Width, float Height) {
		if (bitmap != null && screenWidth != 0 && screenHeight != 0
				&& Height != 0 && Width != 0) {
			float h = AdaptersHeight(screenHeight, Height);
			float w = AdaptersWidth(screenWidth, Width);
			float bitmapWidth = bitmap.getWidth();
			float scaleW = (float) w / bitmapWidth;
			int bitmapHeight = bitmap.getHeight();
			float scaleH = (float) h / bitmapHeight;
			Matrix matrix = new Matrix();
			matrix.postScale(scaleW, scaleH);
			try {
				Bitmap b = bitmap;
				bitmap = Bitmap.createBitmap(b, 0, 0, bitmap.getWidth(),
						bitmap.getHeight(), matrix, true);
				if (b.isRecycled()) {
					b.recycle();
				}
				b = null;
			} catch (Exception e) {
				bitmap = null;
				e.printStackTrace();
			}

		}
		return bitmap;
	}
/**
 * 给定的图片宽度,适配当前屏幕。原图的高度将自动适配
 * @param b 原图
 * @param Width 标准分辨率的宽度
 * @return 适配后的图片
 */
	public final static Bitmap AdaptersBitmapForWidth(Bitmap b, float Width) {
		return AdaptersBitmapForWidth(b, ScreenWidth, Width);
	}
/**
 * 指定图片的宽度,将图片按照给定的屏幕分辨率进行适配
 * @param b 原图
 * @param screenWidth 期待适配的分辨率的宽度
 * @param Width 标准分辨率的宽度
 * @return 适配后的图片
 */
	public final static Bitmap AdaptersBitmapForWidth(Bitmap b,
			int screenWidth, float Width) {
		if (b != null && screenWidth != 0 && Width != 0) {
			float w = AdaptersWidth(screenWidth, Width);
			float bitmapWidth = b.getWidth();
			float scale = (float) w / bitmapWidth;
			Matrix matrix = new Matrix();
			matrix.postScale(scale, scale);
			Bitmap b1 = b;
			b = Bitmap.createBitmap(b1, 0, 0, b1.getWidth(), b1.getHeight(),
					matrix, true);
			if(b1.isRecycled()){
				b1.recycle();
			}
			b1 = null;
			return b;
		} else {
			return b;
		}
	}

	public final static Bitmap AdaptersBitmapForHeight(Bitmap b, float Height) {
		return AdaptersBitmapForHeight(b, ScreenHeight, Height);
	}

	public final static Bitmap AdaptersBitmapForHeight(Bitmap b,
			int screenHeight, float Height) {
		if (b != null && screenHeight != 0 && Height != 0) {
			float H = AdaptersHeight(screenHeight, Height);
			float bitmapHeight = b.getHeight();
			float scale = (float) H / bitmapHeight;
			Matrix matrix = new Matrix();
			matrix.postScale(scale, scale);
			Bitmap b1 = b;
			b = Bitmap.createBitmap(b1, 0, 0, b1.getWidth(), b1.getHeight(),
					matrix, true);
			if(b1.isRecycled()){
				b1.recycle();
			}
			b1 = null;
			return b;
		} else {
			return b;
		}
	}
	/**
	 * 将标准分辨率的高度值,按比例转换为当前屏幕的高度值
	 * @param Height 标准分辨率的高度
	 * @return 适配后的高度值
	 */
	public final static float AdaptersHeight(float Height) {
		return AdaptersHeight(ScreenHeight, Height);
	}
	/**
	 * 将标准分辨率的高度值,按比例转换为给定的屏幕的高度值
	 * @param screenHeight 期待适配的分辨率的高度
	 * @param Height 标准分辨率的高度
	 * @return 适配后的高度值
	 */
	public final static float AdaptersHeight(int screenHeight, float Height) {
		if (screenHeight != 0 && Height != 0) {
			return ((float) (screenHeight * Height) / HEIGHT);
		} else {
			return Height;
		}
	}

	public final static float AdaptersWidth(float Width) {
		return AdaptersWidth(ScreenWidth, Width);
	}

	public final static float AdaptersWidth(int screenWidth, float Width) {
		if (screenWidth != 0 && Width != 0) {
			return ((float) (screenWidth * Width) / WIDTH);
		} else {
			return Width;
		}
	}
}
代码就贴到这里了,需要注意的是在使用该类之前,一定要将手机的分辨率赋值给

//	当前手机的垂直像素
	public static int ScreenHeight;
//	当前手机的水平像素
	public static int ScreenWidth;
这两个类变量。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值