- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在制作一款棋盘游戏,我正在为棋盘使用 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/
我正在尝试设置一个图像,并在短暂的停顿(如闪烁)后将其更改为不同的图像;并且它应该在应用程序启动后自动启动。 public class MainActivity extends ActionBarAc
我需要更改 ImageView 中的图像,然后获取宽度。问题是,它给了我最后一张图片的宽度。 headpic.setImageResource(head[x]); int width = headpi
我有以下代码: public class PainLogger extends Activity implements OnClickListener, OnItemClickListener{
我正在尝试制作我的第一个应用程序 (yahtzee)。当您单击滚动按钮时,我想切换骰子的图像,现在每当我调用 setImageResource 时我都会崩溃。 主要 Activity : public
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
每当我尝试运行该行时,我的程序就会崩溃: d1.setImageResource(R.drawable.clubs9); 我想不通为什么。它似乎与我使用类似代码行的另一个程序相同。我试过添加 getR
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set
我正在制作一款棋盘游戏,我正在为棋盘使用 10x10 GridView。我制作了一个扩展 BaseAdapter 的 ImageAdapter 类,它包含一个整数图标数组(9 个补丁文件),这些用于显
我有一个 imageView,我想显示您当前所在国家/地区的小图标。我可以获得国家代码,但问题是我无法动态更改 imageView 资源。我的图片文件都是小写的(例如:国家代码=US,图片文件=us)
我想根据我的字符串更改 imageview src,我有这样的东西: ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);
我是 java/android 新手,正在制作一个测试应用程序。它有图像按钮,单击时会暂时切换到不同的图像。使用 裁剪原件 android:adjustViewBounds="true" androi
我试图在我的应用程序中显示一个图像,每次按下按钮时该图像都会发生变化。应显示的图像的名称位于我的对象中。我可以通过以下方式获取图像的名称 String nameOfImage = myObhect.g
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我有一个 imageView,我想显示您当前所在国家/地区的小图标。我可以获取国家/地区代码,但问题是我无法动态更改 imageView 资源。我的图像文件都是小写的(例如:国家/地区代码=US,图像
我尝试设置IV_status.setImageResource(R.drawable.red) 但它似乎不起作用,每当调用上面的代码时,整个应用程序都会运行 会崩溃。我也尝试过setBackgroun
我无法找到如何将图片设置为 setImageResource()。目前我可以像这样从 res/drawable 轻松添加: 在 ListViewAdapter 中: imageView = (Imag
我需要有关使用 setImageResource 方法设置随机图像的帮助。在可绘制文件夹中,我有一个名为 photo0.jpg、photo1.jpg...photo99.jpg 的 jpeg 文件。以
我正在尝试从我的 Assets 文件夹中获取图像并将它们设置到我的变量中,称为insectImage。这是我所做的: InsectRecyclerAdapter.InsectHolder insect
我正在构建另一个更大的应用程序,在某些时候我需要动态更改图像,并且我的应用程序崩溃了。因此,为了测试功能,我制作了另一个非常简单的应用程序,它只显示带有代码的图片: package com.examp
我想基于变量以编程方式为 ImageButton 设置 ImageResource。 例如:如果 size=5,我想将 setImageResource 设置为 R.drawable.five
我是一名优秀的程序员,十分优秀!