Android 位图太大,缩小后无法上载到纹理

Android 位图太大,缩小后无法上载到纹理,android,bitmap,android-imageview,Android,Bitmap,Android Imageview,我做了一个大背景的申请。它在Nexus7上运行得非常完美,但在我的Galaxy Nexus上,背景消失了,在logcat中出现了一个错误:位图太大,无法上传到纹理中(…) 我已经阅读并使用了上面的代码。我用来设置背景的代码如下: Display display = getWindowManager().getDefaultDisplay(); width = display.getWidth(); height = display.getHeight(); //---

我做了一个大背景的申请。它在Nexus7上运行得非常完美,但在我的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;
Display Display=getWindowManager().getDefaultDisplay();
宽度=display.getWidth();
高度=display.getHeight();
//-------------------------------------------------------------------------------------------------------------
ImageView achtergrond=(ImageView)findViewById(R.id.achtergrond);
setImageBitmap(decodeSampledBitmapFromResource(getResources(),R.drawable.achtergrond,width,height));
} (...)
公共静态位图解码SampledBitMapFromResource,
输入要求宽度,输入要求高度){
//使用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=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}

我的R.drawable.achtergrond的分辨率为:1100x539。此外,如果我注释掉该规则(setBitmapImage),则错误停止。我的Galaxy Nexus上始终没有显示背景,而Nexus 7上却显示了背景。这张图像太大了,分辨率很好。背景图像目前位于drawable文件夹中


谢谢你提前帮助我

将背景移动到
可绘制的nodpi
文件夹

正如你在《到》中所说的,每台设备都对图像在屏幕上作为纹理渲染的最大尺寸有限制;宽度和高度通常为2048。但是,如果您将drawable放在没有密度限定符的文件夹中,Android会假定drawable是mdpi,并根据其他密度放大或缩小它。例如,如果您的屏幕具有xhdpi密度,则可绘制屏幕将放大两倍:1100 x 2=2200,这对于2048限制来说太大了