我制作了一个背景很大的应用程序。它在 Nexus 7 上完美运行,但在我的 Galaxy Nexus 上,背景消失了,并且在 logcat 中给出了错误:位图太大而无法上传到纹理中(...)。
我已经阅读了这个并且还使用了那里的代码。我用来设置背景的代码是这样的:
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
//-------------------------------------------------------------------------------------------------------------
ImageView achtergrond = (ImageView)findViewById(R.id.achtergrond);
achtergrond.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.achtergrond, width, height));
} (...)
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;
}
我的 R.drawable.achtergrond 的分辨率是:1100x539。此外,如果我注释掉该规则(setBitmapImage),那么错误就会停止。在我的 Nexus 7 上显示时,我的 Galaxy Nexus 上始终没有显示背景。图像很大,分辨率很高。背景图像目前位于可绘制文件夹中。
感谢您提前帮助我!