Android如何解决使用图片时候但内存泄漏

Android如何解决使用图片时候但内存泄漏

在网上查了一下,Android分配给图片但内存只有8M,所以当很多图片使用但时候就设计到了内存泄漏问题,网上只是简单但说了要recycle()一下,那是在使用Bitmap的时候,那么如果使用图片的时候没有涉及到Bitmap呢?
下面的是一个程序,可以运行,但是如果图片较多会出现内存泄漏,请问怎么解决呢

Java code?package com.loulijun.imageswitcher;   import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import 论文网  android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory;   public class ImageSwitcherActivity extends Activity implements ViewFactory {     private ImageSwitcher is;     private Gallery gallery;     //图片资源     private Integer[] thumbs = {             R.drawable.yuanyuan01,             R.drawable.yuanyuan02,             R.drawable.yuanyuan03,             R.drawable.yuanyuan04,             R.drawable.yuanyuan05,             R.drawable.yuanyuan06,             R.drawable.yuanyuan07,             R.drawable.yuanyuan08     };           private Integer[] imgIds = {             R.drawable.yuanyuan01,             R.drawable.yuanyuan02,             R.drawable.yuanyuan03,             R.drawable.yuanyuan04,             R.drawable.yuanyuan05,             R.drawable.yuanyuan06,             R.drawable.yuanyuan07,             R.drawable.yuanyuan08     };     @Override    public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         //设置为全屏模式         requestWindowFeature(Window.FEATURE_NO_TITLE);         setContentView(R.layout.main);                   is = (ImageSwitcher)findViewById(R.id.switcher);         is.setFactory(this);         //淡入淡出         is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));         is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));                   gallery = (Gallery)findViewById(R.id.gallery);         gallery.setAdapter(new ImageAdapter(this));         gallery.setOnItemSelectedListener(new Gallery.OnItemSelectedListener()         {               public void onItemSelected(AdapterView<?> arg0, View arg1,                     int arg2, long arg3) {                 is.setImageResource(imgIds[arg2]);                               }               public void onNothingSelected(AdapterView<?> arg0) {                 // TODO Auto-generated method stub                               }                       });     }     public View makeView() {         ImageView i = new ImageView(this);           i.setBackgroundColor(0xFF000000);           i.setScaleType(ImageView.ScaleType.FIT_CENTER);           i.setLayoutParams(new ImageSwitcher.LayoutParams(             LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));           return i;     }     public class ImageAdapter extends BaseAdapter     {         //重写构造方法         private Context context;         public ImageAdapter(Context c)         {             context = c;         }                   //返回图片的个数         public int getCount() {             // TODO Auto-generated method stub             return thumbs.length;         }           public Object getItem(int position) {             // TODO Auto-generated method stub             return position;         }           public long getItemId(int position) {             // TODO Auto-generated method stub             return position;         }           public View getView(int position, View convertView, ViewGroup parent) {             ImageView iv = new ImageView(context);             iv.setImageResource(thumbs[position]);             iv.setAdjustViewBounds(true);             iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));                           return iv;         }               }       }
最后的解决办法是把美工压缩图片质量,而且不能用JPG的因为JPG本身就是压缩格式,用的时候会解码出来。


还有就是,直接引用的时候,其实也是后台通过bitmap解码来的
软引用可以降低fail率  不妨一试


Java code?/**  * 该类演示了Soft Reference的应用  */package cn.javatx;   import java.lang.ref.SoftReference;     public class softReference {       /**      * @param args      */    public static void main(String[] args) {         // TODO Auto-generated method stub         A a = new A();                   //使用a         a.test();           //使用完了a,将它设置为soft引用类型,并且释放强引用         SoftReference sr = new SoftReference(a);         a = null;                   //下次使用         if (sr != null) {             a = (A)sr.get();             a.test();         } else {             //GC由于低内存,已释放a,因此需要重新装载             a = new A();             a.test();             a = null;             sr = new SoftReference(a);         }     }   }   class A {     public void test() {         System.out.println("Soft Reference test");     } } 
内存泄露的地方是这个吧
new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)

每次getView刷新都得new一个对象
试图定义一个全局的LayoutParams对象,只初始化一次,试试。。。
其他地方频繁new的地方也有,都改正以下,看还会不会泄露

Copyright © 2007-2012 www.chuibin.com 六维论文网 版权所有