Android如何解决因使用大量图片而导致的内存泄露问题?

我a你44
2013-10-13 · TA获得超过2878个赞
知道小有建树答主
回答量:1011
采纳率:0%
帮助的人:2382万
展开全部
不过因使用大量图片引起的内存泄露的确是个问题,所以要解决它才能以防后患,希望各位高手能够解答。 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) { // 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; } } }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式