我是位图,缩小版和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));}
问题?我不知道。我是新手,所以我问你。谢谢:))
答案 0 :(得分:1)
代码几乎没问题,你混淆了#id;查看ID' (R.id.stuff_i_defined_in_xml)和'资源ID' (例如R.drawable.my_png_file)。试试:
image9View.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.drawable.faqImage, 50, 50));
确保您有一个名为" faqImage.png"的文件。在drawables文件夹中(res / drawable *之一)。