我最近一直在2.1 SDK(版本7)中工作,并创建了一些图像,这些图像将通过ImageSwitcher显示,并通过库中的一个示例应用程序进行更改。我的图像都在200Kb左右,我注意到当我切换它们时,Android崩溃时出现Out-Of-Memory错误。我知道堆大小通常是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 Sample:
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左右(这意味着我的分辨率会因较大的屏幕而丢失),我的应用程序不会崩溃。
任何人都可以提供一些有关为什么会发生这种情况的更多信息,并且更好,一种有效的解决方法吗?
答案 0 :(得分:4)
试试这段代码:
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;
BitmapFactory.Options options=new BitmapFactory.Options();
options.inPurgeable=true; //bitmap can be purged to disk
Bitmap bm=BitmapFactory.decodeResource(mContext.getResources(), mThumbIds[position], options);
iv.setImageBitmap(bm);
return iv;
}
这里的关键点是BitmapFactory.Options.inPurgeable = true;
(这是自我解释) - 你也可以与其他选项一起玩 - 有时它有帮助
答案 1 :(得分:1)
不要致电System.gc()。显着降低性能。请勿在UI线程中执行此操作。 NEVER!