Mar*_*arc 2 android bitmap android-imageview
我做了一个有很大背景的应用程序.它在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;
Run Code Online (Sandbox Code Playgroud)
}
我的R.drawable.achtergrond的分辨率是:1100x539.此外,如果我注释掉该规则(setBitmapImage),那么错误就会停止.整个时间背景都没有显示在我的Galaxy Nexus上,而它确实显示在我的Nexus 7上.图像很大,可以达到很好的分辨率.背景图像目前位于drawable文件夹中.
谢谢你提前帮助我!
| 归档时间: |
|
| 查看次数: |
2556 次 |
| 最近记录: |