gpt4 book ai didi

java - Android 位图缩小不工作

转载 作者:太空狗 更新时间:2023-10-29 14:07:59 25 4
gpt4 key购买 nike

我对位图、缩小比例和 Java 一般不熟悉,所以请比平时更深入地解释一下。谢谢你。哈哈

我的问题:我正在尝试缩小图像以删除那个讨厌的“Java.lang.OutOfMemory”错误!当我尝试缩小比例时,当我启动应用程序时,那里没有图像,而不是那里的图像。

也许你能帮上忙:)

我的两种方法:

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 方法(我确定这是一些非常愚蠢的错误,所以请对我宽容点(大声笑抱歉)):

ImageView image9View;
ImageView image8View;
ImageView image7View;
ImageView image6View;
ImageView image5View;
ImageView image4View;
ImageView image3View;
ImageView image2View;
ImageView image1View;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);




image9View = (ImageView)findViewById(R.id.faqImage);
image8View = (ImageView)findViewById(R.id.foodIcon);
image7View = (ImageView)findViewById(R.id.bracketsIcon);
image6View = (ImageView)findViewById(R.id.teamsIcon);
image5View = (ImageView)findViewById(R.id.playersIcon);
image4View = (ImageView)findViewById(R.id.gamesIcon);
image3View = (ImageView)findViewById(R.id.homeIcon);
image2View = (ImageView)findViewById(R.id.settingsIcon);
image1View = (ImageView)findViewById(R.id.alert);


image9View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.faqImage, 50, 50));
image8View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.foodIcon, 50, 50));
image7View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.bracketsIcon, 50, 50));
image6View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.teamsIcon, 50, 50));
image5View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.playersIcon, 50, 50));
image4View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.gamesIcon, 50, 50));
image3View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.homeIcon, 50, 50));
image2View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.settingsIcon, 50, 50));
image1View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.alert, 50, 50));}

问题是什么?我不知道。我对这东西很陌生,所以我问你。谢谢:)

最佳答案

代码几乎没问题,您混淆了“ View ID”(R.id.stuff_i_defined_in_xml) 和“资源 ID”(例如 R.drawable.my_png_file)。尝试:

image9View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.drawable.faqImage, 50, 50));

确保在可绘制文件夹(res/drawable* 之一)中有一个名为“faqImage.png”的文件。

关于java - Android 位图缩小不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31416222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com