我将 4x4 imageView 放入活动(BoardActivity)中,用户可以通过单击它们来更改图像。使用 HTC Desire (Android 2.2.2),我在大约 30 分钟的密集使用中遇到 OOM(内存不足)-编辑:此活动第 16 次开始-,但没有其他设备产生这个(android 2.1和android 2.2.1)。我是否可能在使用位图/图像视图时犯了一些错误并导致了此错误?
首先,我将所有资源 ID 加载到地图中:
private Map<String, Integer> imageResourceMap;
imageResourceMap = new HashMap<String, Integer>();
imageResourceMap.put("a", R.drawable.a);
imageResourceMap.put("b", R.drawable.b);
imageResourceMap.put("c", R.drawable.c);
//... I store 55 drawable's resourceId in this map
然后我调整每个图像的大小并将其保存为位图,在地图中表示:
private static Map<String, Bitmap> imageBitmap;
private void loadBitmaps(int imagesSize) {
for (String s : imageResourceMap.keySet()) {
Bitmap tmp = getBitmapFromRes(imageResourceMap.get(s), imagesSize);
imageBitmap.put(s, tmp);
}
}
private Bitmap getBitmapFromRes(int resId, int imageSize) {
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
InputStream fis = getResources().openRawResource(resId);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > imageSize || o.outWidth > imageSize) {
scale = (int) Math.pow(2, (int) Math.round(Math.log(imageSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = getResources().openRawResource(resId);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
我将 imageView 保存在一个数组中,并使用此函数初始化所有图像:
private static ImageView[][] imageViews;
private ImageView getImage(String name) {
MyImageView item = new MyImageView(this, i, j, c + "");
item.setImageBitmap(imageBitmap.get(name));
item.setAdjustViewBounds(true);
return item;
}
当我需要更改图像时,我只需更改其资源:
imageViews[i][j].setImageBitmap(imageBitmap.get("a"));
在完成活动之前,我回收了位图:
private void recycleImageBitmaps() {
for (Bitmap b : imageBitmap.values()) {
if (b != null) {
b.recycle();
}
}
}
在 AndroidManifest.xml 中,我声明此活动为“singleTask”:
<activity android:name=".game.board.BoardActivity" android:launchMode="singleTask">
</activity>
在此应用程序(游戏)中,我们多次重新打开此活动......
我做错了什么?这会导致内存不足错误吗?
更正更正了 getBitmapFromRes,如下所示:
private Bitmap getBitmapFromRes(int resId, int imageSize) {
Bitmap tmp = null;
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
InputStream fis = getResources().openRawResource(resId);
tmp = BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > imageSize || o.outWidth > imageSize) {
scale = (int) Math.pow(2, (int) Math.round(Math.log(imageSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = getResources().openRawResource(resId);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(tmp != null){
tmp.recycle();
tmp = null;
}
}
return b;
}
HTC在该活动第11次开始时仍然死机。
EDIT:该活动(BoardActivity)从一个活动(MenuActivity)启动,该活动有4个imageButton,并且位于Tabhost活动中。 imageButtons 声明如下所示:
<ImageButton
android:id="@+id/game_menu_CreateButton"
android:layout_width="120dip"
android:layout_height="120dip"
android:layout_alignRight="@+id/textView1"
android:layout_alignTop="@+id/textView1"
android:background="@drawable/create"
android:layout_marginRight="1sp"
android:layout_marginTop="10sp"
/>
当我从 MenuActivity 启动 BoardActivity 时,我不调用finish()
在 MenuActivity 中,当我打电话时finish()
在 BoardActivity 中,我没有启动新的意图,因此它只是返回到已经打开的 MenuActivity。到了第 16 轮,我就 OOM 了。