[android] 练习使用ListView(三)详解手机开发

解决OOM和图片乱序问题

[android] 练习使用ListView(三)详解手机开发

package com.android.test; 
 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
 
import com.nostra13.universalimageloader.core.ImageLoader; 
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; 
import com.nostra13.universalimageloader.core.assist.FailReason; 
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener; 
 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.util.LruCache; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
    private ListView lv_images; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        lv_images = (ListView) findViewById(R.id.lv_images); 
         
        ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(this); 
        ImageLoader.getInstance().init(configuration); 
         
        ListAdapter adapter = new ImageAdapter(); 
        lv_images.setAdapter(adapter); 
    } 
    /** 
     * 适配器 
     * @author taoshihan 
     * 
     */ 
    class ImageAdapter extends BaseAdapter { 
        private ListView mListView; 
        private LruCache<String, BitmapDrawable> mImageCache; 
        public ImageAdapter() { 
            int maxSize=(int) (Runtime.getRuntime().maxMemory()/1024)/8; 
            mImageCache=new LruCache<String, BitmapDrawable>(maxSize){ 
                protected int sizeOf(String key, BitmapDrawable drawable) { 
                    return drawable.getBitmap().getByteCount()/1024;  
                }; 
            }; 
        } 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
            //第三个参数就是ListView 
            if(mListView==null){ 
                mListView=(ListView) parent; 
            } 
             
            String url=(String) getItem(position); 
            View view; 
            if(convertView!=null){ 
                view=convertView; 
            }else{ 
                view=View.inflate(getApplicationContext(), R.layout.image_item, null); 
            } 
            ImageView imageView=(ImageView) view.findViewById(R.id.tv_image); 
            //给ImageView设置标签 
            imageView.setTag(url); 
            BitmapDrawable bitmapDrawable=getBitmapFromMemoryCache(url); 
            if(bitmapDrawable!=null){ 
                //读取缓存图片 
                imageView.setImageDrawable(bitmapDrawable); 
            }else{ 
                //读取网络图片 
                ImageWorkerTask task=new ImageWorkerTask(mListView,url); 
                task.execute(url); 
            } 
            return view; 
        } 
        /** 
         * 从缓存中读取图片 
         * @param key 
         * @return  
         */ 
        public BitmapDrawable getBitmapFromMemoryCache(String key) { 
            if(mImageCache.get(key)!=null){ 
                return mImageCache.get(key); 
            } 
            return null; 
        } 
        public void addBitmapToMemoryCache(String key,BitmapDrawable drawable){ 
            if(getBitmapFromMemoryCache(key)==null){ 
                mImageCache.put(key, drawable); 
            } 
        } 
        @Override 
        public int getCount() { 
            // TODO Auto-generated method stub 
            return Images.IMAGE_URLS.length; 
        } 
 
        @Override 
        public Object getItem(int position) { 
            return Images.IMAGE_URLS[position]; 
        } 
 
        @Override 
        public long getItemId(int position) { 
            // TODO Auto-generated method stub 
            return position; 
        } 
        /** 
         * 异步任务 
         * @author taoshihan 
         * 
         */ 
        class ImageWorkerTask extends AsyncTask<String, Void, BitmapDrawable>{ 
            private ImageView imageView; 
            public ImageWorkerTask(ListView parent,String imageUrl) { 
                //在ListView中通过标签找到ImageView控件 
                this.imageView=(ImageView) parent.findViewWithTag(imageUrl); 
            } 
            @Override 
            protected BitmapDrawable doInBackground(String... params) { 
                String imageUrl=params[0]; 
                Bitmap bitmap=downLoadBitmap(imageUrl); 
                BitmapDrawable drawable=new BitmapDrawable(getApplicationContext().getResources(),bitmap); 
                addBitmapToMemoryCache(imageUrl, drawable); 
                return drawable; 
            } 
            //下载图片 
            private Bitmap downLoadBitmap(String imageUrl) { 
                Bitmap bitmap=null; 
                try { 
                    URL url=new URL(imageUrl); 
                    HttpURLConnection conn=(HttpURLConnection) url.openConnection(); 
                    conn.setConnectTimeout(10000); 
                    InputStream is=conn.getInputStream(); 
                    bitmap=BitmapFactory.decodeStream(is); 
                } catch (Exception e) { 
                } 
                return bitmap; 
            } 
            //下载完成的 
            @Override 
            protected void onPostExecute(BitmapDrawable result) { 
                if(imageView!=null&&result!=null){ 
                    imageView.setImageDrawable(result); 
                } 
            } 
             
        } 
 
    } 
    static class Images{ 
        public final static String[] IMAGE_URLS = new String[]{ 
            "http://img.my.csdn.net/uploads/201508/05/1438760758_3497.jpg",     
            "http://img.my.csdn.net/uploads/201508/05/1438760758_6667.jpg",   
            "http://img.my.csdn.net/uploads/201508/05/1438760757_3588.jpg",   
            "http://img.my.csdn.net/uploads/201508/05/1438760756_3304.jpg",   
            "http://img.my.csdn.net/uploads/201508/05/1438760755_6715.jpeg",   
            "http://img.my.csdn.net/uploads/201508/05/1438760726_5120.jpg",   
            "http://img.my.csdn.net/uploads/201508/05/1438760726_8364.jpg",   
            "http://img.my.csdn.net/uploads/201508/05/1438760725_4031.jpg",   
            "http://img.my.csdn.net/uploads/201508/05/1438760724_9463.jpg",   
            "http://img.my.csdn.net/uploads/201508/05/1438760724_2371.jpg",   
            "http://img.my.csdn.net/uploads/201508/05/1438760707_4653.jpg" 
        }; 
    } 
}

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/app/5369.html

(0)
上一篇 2021年7月16日 23:46
下一篇 2021年7月16日 23:46

相关推荐

发表回复

登录后才能评论