我製作了一個具有大背景的應用程序。它在Nexus 7上完美運行,但在我的Galaxy Nexus上,背景消失了,並在logcat中給出了錯誤:位圖太大而無法上傳到紋理(...)。位圖太大而無法在縮小後上傳到紋理中
我讀過this,也使用了那裏的代碼。我常常把我的後臺代碼是這樣的:
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),那麼錯誤就會停止。整個過程中,背景沒有出現在我的Galaxy Nexus上,而它顯示在我的Nexus 7上。圖像非常重要,可以達到很好的分辨率。此時背景圖片在可繪製文件夾中。
感謝您提前幫助我!