Java Android加载图形
我有个问题。我的主菜单/开始屏幕有一个大的图形。这会导致某些旧设备出现内存不足异常。这是一个分辨率为1920*1080的PNG文件,我使用的是图像格式RGB565。您知道如何减少使用的ram吗?如前所述,您可以缩小图像的比例,但如果出于某种原因您不想或不能这样做,您可能需要检查该链接: 你也可以加上Java Android加载图形,java,android,memory,graphic,Java,Android,Memory,Graphic,我有个问题。我的主菜单/开始屏幕有一个大的图形。这会导致某些旧设备出现内存不足异常。这是一个分辨率为1920*1080的PNG文件,我使用的是图像格式RGB565。您知道如何减少使用的ram吗?如前所述,您可以缩小图像的比例,但如果出于某种原因您不想或不能这样做,您可能需要检查该链接: 你也可以加上 largeHeap=true 为了增加一点内存,但它只能在新设备上工作,Android 2.x不支持这一点这就是您想要的: BitmapFactory.Options.inSampleSize
largeHeap=true
为了增加一点内存,但它只能在新设备上工作,Android 2.x不支持这一点这就是您想要的:
BitmapFactory.Options.inSampleSize
如果设置为true,则生成的位图将分配其
像素,以便在系统需要回收时可以清除它们
记忆。在这种情况下,当需要再次访问像素时
(例如,绘制位图,调用getPixels()),它们将
自动重新解码。要进行重新解码,位图必须
通过共享对
输入或复制。这种区别是由
不可输入共享。如果这是真的,那么位图可能会保持浅颜色
对输入的引用。如果为false,则位图将显示
显式复制输入数据,并保留该数据。即使
如果允许共享,则实施人员仍可能决定进行深入研究
输入数据的副本
资料来源:
因此,基本上,您可以根据屏幕分辨率和可用RAM调整inSampleSize
,以便始终为给定设备提供适当版本的位图。这将防止发生OutOfMemoryError
错误
下面是一个如何利用它的示例:
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
公共静态位图解码SampledBitMapFromResource(资源res,int resId,
输入要求宽度,输入要求高度){
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码资源(res、resId、options);
//计算样本大小
options.inSampleSize=计算样本大小(options、reqWidth、reqHeight);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeResource(res、resId、options);
}
公共静态整数计算示例大小(
BitmapFactory.Options选项、int reqWidth、int reqHeight){
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
//计算高度和宽度与所需高度和宽度的比率
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
//选择最小比率作为采样值,这将保证
//最终图像的两个尺寸均大于或等于
//要求的高度和宽度。
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
资料来源:
该链接下还有更多信息,因此我建议您查看。缩小图像大小。你到底为什么需要一个这么大的图像来安装一个小型安卓设备?