我最近一直在使用 2.1 SDK(版本 7)并创建了一些图像,这些图像将通过 ImageSwitcher 显示并通过画廊进行更改,就像在其中一个示例应用程序中一样。我的图像都在 200Kb 左右,我注意到当我切换它们时,Android 会因内存不足错误而崩溃。我知道堆大小通常为 16Mb,所以我不明白为什么 200Kb 的图像会使应用程序崩溃。我在 drawables 目录中有 21 个图像,但一次只显示一个。
这是我的标记:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF">
<ScrollView android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="17dp">
<ImageSwitcher android:id="@+id/switcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/adView"
android:background="#FFFFFF"/>
</ScrollView>
<Gallery android:id="@+id/gallery"
android:background="#66000000"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="13dp"/>
</RelativeLayout>
我的代码主要来自 ImageSwitcher/Gallery 的 Google 示例:
public class HomeActivity extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory{
private Integer[] mThumbIds = {
R.drawable.a3035, R.drawable.a40,
R.drawable.a45, R.drawable.a50,
R.drawable.a55, R.drawable.a60,
R.drawable.a70, R.drawable.a80,
R.drawable.a90,R.drawable.a100,
R.drawable.a110,R.drawable.a120,
R.drawable.a130,R.drawable.a140,
R.drawable.a150,R.drawable.a160,
R.drawable.a170,R.drawable.a180,
R.drawable.a190,R.drawable.a200210,
R.drawable.a220300};
private Integer[] mImageIds = {
R.drawable.aird3035, R.drawable.aird40, R.drawable.aird45,
R.drawable.aird50, R.drawable.aird55, R.drawable.aird60,
R.drawable.aird70, R.drawable.aird80,R.drawable.aird90,
R.drawable.aird100, R.drawable.aird110, R.drawable.aird120,
R.drawable.aird130, R.drawable.aird140,R.drawable.aird150,
R.drawable.aird160, R.drawable.aird170,R.drawable.aird180,
R.drawable.aird190, R.drawable.aird200210,R.drawable.aird220300};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
scroll = (ScrollView)findViewById(R.id.scroll);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
@Override
public void onPause()
{
super.onPause();
System.gc();
}
@Override
public void onDestroy()
{
super.onDestroy();
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
scroll.scrollTo(0,0);
System.gc();
}
public void onNothingSelected(AdapterView parent) {
}
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFFFFFF);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return i;
}
private ImageSwitcher mSwitcher;
private ScrollView scroll;
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) { // create new view
convertView = new ImageView(mContext);
convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
ImageView iv = (ImageView) convertView;
iv.setImageResource(mThumbIds[position]);
return convertView;
}
private Context mContext;
}
}
如果您注意到 OnItemSelected 方法,我什至会强制调用垃圾收集器,但我仍然崩溃。如果我将图像缩小到每个 93Kb 左右(这意味着我失去了更大屏幕尺寸的分辨率),我的应用程序不会崩溃。
谁能提供一些关于为什么会发生这种情况以及更好的解决方法的见解?