手游《奔跑吧?骚年!》技术分享(二):图片适配
2014-07-13 00:31
344 查看
做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;这两个类变量。。。
相关文章推荐
- 手游《奔跑吧?骚年!》技术分享(四):矩形碰撞检测
- 手游《奔跑吧?骚年!》技术分享(三):文件处理
- 手游《奔跑吧?骚年!》技术分享(一):开篇
- 手游《奔跑吧?骚年!》技术分享(五):三角形碰撞检测
- C#技术分享【PDF转换成图片——13种方案】(2013-07-25重新整理)
- C#技术分享【PDF转换成图片——11种方案】
- C#技术分享【PDF转换成图片――13种方案】
- C#技术分享【Word转换成图片和PDF——3种方案】
- [技术分享] .NET下 , 上传图片的处理方式 , 贴上代码 .
- 迈向Silverlight之路-图片翻转裁剪技术分享(原创)
- C#技术分享【PDF转换成图片——13种方案】
- C#技术分享【PDF转换成图片——11种方案】
- C#技术分享【Word转换成图片和PDF——2种方案】
- Lightswitch相关技术文章(分享图片)
- C#技术分享【PDF转换成图片——11种方案】(转)
- JavaScript图片放大技术(放大镜)实现代码分享
- C#技术分享【Word转换成图片和PDF——2种方案】
- C#技术分享【PDF转换成图片——11种方案】
- Cocos2d-x手游技术分享(1)-【天天打蚊子】数据存储与音效篇
- C#技术分享【PDF转换成图片——13种方案】(2013-07-25重新整理)