gpt4 book ai didi

android - setImageResource 内存不足错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:39:25 26 4
gpt4 key购买 nike

我正在制作一款棋盘游戏,我正在为棋盘使用 10x10 GridView。我制作了一个扩展 BaseAdapter 的 ImageAdapter 类,它包含一个整数图标数组(9 个补丁文件),这些用于显示棋盘正方形的图像。图标存放在res/drawable文件夹中,大小为629X629,平均大小约5KB。

我的 ImageAdapter 类有以下 getView() 方法,它基本上回收相同的 View 以节省内存:

编辑:(我包含了在游戏 Activity 中调用的 changeIcon 方法)

public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else{
imageView = (ImageView) convertView;
}


imageView.setImageResource(mThumbIds[position]);
return imageView;
}

public void changeIcon(int x, int y, Integer icon){
int position = (9-y)*10 + x;
mThumbIds[position] = icon;
getView(position, currentView, null);
}

我有一个名为 Game 的单独类,用于处理游戏逻辑。我将棋子存储在一个 Piece[][] 数组中,其中 Piece 是我创建的另一个类,用于保存(你猜对了)游戏棋子的数据。相关的是处理棋子移动的方法 move(int xFrom, int yFrom, int xTo, int yTo)。我可以整天移动零件,一切都很好。

但是,一旦我将一 block 移到另一 block ,应用程序就会崩溃。预期的行为是创建一个新 fragment 。发生这种情况的代码如下:

public boolean move(int xFrom, int yFrom, int xTo, int yTo){

boolean success = false;
Piece pieceToMove = getPiece(xFrom,yFrom);
Piece pieceAtDest = getPiece(xTo,yTo);
int moveTeam = pieceToMove.getTeam();

if(isLegalMove(xFrom, yFrom, xTo, yTo, pieceToMove)&&(turn == moveTeam)){
if( pieceAtDest == null)
{
/*I do something when the destination piece is null;
this block causes no problems */
}
else{
success = true;
pieceToMove.merge();
position[xTo][yTo] = pieceToMove;
position[xFrom][yFrom] = null;
}
}
return success;
}

因此,有问题的调用是 pieceToMove.merge()。 Piece 类中的方法 merge() 只是更改该类中的字段 type(该 piece 变成新的东西),然后调用该类的方法 setIcon()。此方法根据类型的值设置类的icon 字段。并且,如上所述,图标是整数,指的是 res/drawable 中的 9-patch 文件。

最后,方法 move(int xFrom, int yFrom, int xTo, int yTo) 从 Activity GameBoardActivity 调用,在成功移动后,Activity 要求 ImageAdapter(称为 adapter)重绘棋盘,如下:

boolean success = game.move(xFrom,yFrom,xTo,yTo);

if(success){

Integer destIcon = game.getPiece(xTo, yTo).getIcon();
Piece pieceAtDep = game.getPiece(xFrom, yFrom);
Integer depIcon;
if(pieceAtDep == null)
depIcon = R.drawable.square;
else
depIcon = game.getPiece(xFrom, yFrom).getIcon();
adapter.changeIcon(xTo,yTo,destIcon);
adapter.changeIcon(xFrom,yFrom,depIcon);
gridView.setAdapter(adapter);
}

Logcat 表示导致“致命信号 11”和“6330272 字节分配内存不足”的行是 imageView.setImageResource(mThumbIds[position]); 中的行ImageAdapter 的 getView 方法。

所以,就像我说的,一切都很顺利,直到我需要合并两个部分,然后我得到了内存不足的错误。还值得注意的是,此合并行为在应用程序的早期迭代中运行良好。

我现在应该提到标准免责声明,因为在使用 java/android 进行编码时我是一个完全的初学者,并且我已经查看了与类似问题相关的其他问题,但似乎没有其他人在处理他们的位图我也是。

非常感谢任何帮助。非常感谢。

更新

通过进一步测试,我注意到另一个奇怪之处,即有时会出现问题,有时不会,而且我无法确定在某些情况下导致崩溃的原因,而在其他情况下则不会。更准确地说,执行完全相同的 Action 顺序可能会也可能不会导致崩溃。这很神秘。

最佳答案

在您的代码中,mThumbIds 是可绘制对象的Id。你应该做的是你必须通过以下代码创建特定图像的 Thumb

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;
}

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);
}

使用此代码,

decodeSampledBitmapFromResource(getResources(),R.drawable.xyz, 100, 100);

在这里,您提供的样本量为 100 * 100。因此将创建这样大小的缩略图。

关于android - setImageResource 内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16183635/

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