Java Android位图缩小不起作用
我对位图、缩小尺寸和Java一般来说都是新手,所以请比平时更深入地解释一下。非常感谢。哈哈 我的问题: 我正在尝试缩小图像的比例,以消除令人讨厌的“Java.lang.OutOfMemory”错误!当我尝试缩小规模时,当我启动应用程序时,那里没有图像,而不是图像。 也许你能帮忙:) 我的两种方法:Java Android位图缩小不起作用,java,android,bitmap,Java,Android,Bitmap,我对位图、缩小尺寸和Java一般来说都是新手,所以请比平时更深入地解释一下。非常感谢。哈哈 我的问题: 我正在尝试缩小图像的比例,以消除令人讨厌的“Java.lang.OutOfMemory”错误!当我尝试缩小规模时,当我启动应用程序时,那里没有图像,而不是图像。 也许你能帮忙:) 我的两种方法: public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int re
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) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
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);
}
还有我的onCreate方法(我确信这是一个非常愚蠢的错误,所以请对我放松点(lol对不起)):
问题出在哪里?我不知道。我是新来的,所以我问你。谢谢:)代码几乎没有问题,您混淆了“视图id”(R.id.stuff\u i\u xml中定义的)和“资源id”(例如R.drawable.my\u png\u文件)。尝试:
image9View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.drawable.faqImage, 50, 50));
确保在drawables文件夹(res/drawable*之一)中有一个名为“faqImage.png”的文件
image9View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.drawable.faqImage, 50, 50));