Android异步加载图像(含线程池,缓存方法)

35 篇文章 0 订阅
17 篇文章 0 订阅
package com.bshark.supertelphone.activity;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import com.bshark.supertelphone.R;
import com.bshark.supertelphone.ui.adapter.util.AsyncImageLoader;
import com.bshark.supertelphone.ui.adapter.util.AsyncImageLoader3;

import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class LazyLoadImageActivity extends Activity {
       final Handler handler=new Handler();
      final Handler handler2=new Handler(){
          @Override
          public void handleMessage(Message msg) {
             ((ImageView) LazyLoadImageActivity.this.findViewById(msg.arg1)).setImageDrawable((Drawable)msg.obj);
          }
      };
private ExecutorService executorService = Executors.newFixedThreadPool(5);    //固定五个线程来执行任务
    private AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
    private AsyncImageLoader3 asyncImageLoader3 = new AsyncImageLoader3();


@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
//  loadImage("http://www.chinatelecom.com.cn/images/logo_new.gif", R.id.image1);
//  loadImage("http://www.baidu.com/img/baidu_logo.gif", R.id.image2);
//  loadImage("http://cache.soso.com/30d/img/web/logo.gif", R.id.image3);
//        loadImage("http://www.baidu.com/img/baidu_logo.gif", R.id.image4);
//  loadImage("http://cache.soso.com/30d/img/web/logo.gif", R.id.image5);

        loadImage2("http://www.chinatelecom.com.cn/images/logo_new.gif", R.id.image1);
  loadImage2("http://www.baidu.com/img/baidu_logo.gif", R.id.image2);
  loadImage2("http://cache.soso.com/30d/img/web/logo.gif", R.id.image3);
        loadImage2("http://www.baidu.com/img/baidu_logo.gif", R.id.image4);
  loadImage2("http://cache.soso.com/30d/img/web/logo.gif", R.id.image5);
//        loadImage3("http://www.chinatelecom.com.cn/images/logo_new.gif", R.id.image1);
//  loadImage3("http://www.baidu.com/img/baidu_logo.gif", R.id.image2);
//  loadImage3("http://cache.soso.com/30d/img/web/logo.gif", R.id.image3);
//        loadImage3("http://www.baidu.com/img/baidu_logo.gif", R.id.image4);
//  loadImage3("http://cache.soso.com/30d/img/web/logo.gif", R.id.image5);

//        loadImage4("http://www.chinatelecom.com.cn/images/logo_new.gif", R.id.image1);
//  loadImage4("http://www.baidu.com/img/baidu_logo.gif", R.id.image2);
//  loadImage4("http://cache.soso.com/30d/img/web/logo.gif", R.id.image3);
//        loadImage4("http://www.baidu.com/img/baidu_logo.gif", R.id.image4);
//  loadImage4("http://cache.soso.com/30d/img/web/logo.gif", R.id.image5);

//        loadImage5("http://www.chinatelecom.com.cn/images/logo_new.gif", R.id.image1);
//        //为了测试缓存而模拟的网络延时
//        SystemClock.sleep(2000);
//  loadImage5("http://www.baidu.com/img/baidu_logo.gif", R.id.image2);
//        SystemClock.sleep(2000);
//  loadImage5("http://cache.soso.com/30d/img/web/logo.gif", R.id.image3);
//        SystemClock.sleep(2000);
//        loadImage5("http://www.baidu.com/img/baidu_logo.gif", R.id.image4);
//        SystemClock.sleep(2000);
//  loadImage5("http://cache.soso.com/30d/img/web/logo.gif", R.id.image5);
//        SystemClock.sleep(2000);
//         loadImage5("http://www.baidu.com/img/baidu_logo.gif", R.id.image4);
}

@Override
protected void onDestroy() {
  executorService.shutdown();
  super.onDestroy();
}
    //线程加载图像基本原理
    private void loadImage(final String url, final int id) {
         handler.post(new Runnable() {
                public void run() {
                    Drawable drawable = null;
                    try {
                        drawable = Drawable.createFromStream(new URL(url).openStream(), "image.png");
                    } catch (IOException e) {
                    }
                    ((ImageView) LazyLoadImageActivity.this.findViewById(id)).setImageDrawable(drawable);
                }
            });
    }
     //采用handler+Thread模式实现多线程异步加载
     private void loadImage2(final String url, final int id) {
         Thread thread = new Thread(){
             @Override
             public void run() {
               Drawable drawable = null;
                    try {
                        drawable = Drawable.createFromStream(new URL(url).openStream(), "image.png");
                    } catch (IOException e) {
                    }

                Message message= handler2.obtainMessage() ;
                 message.arg1 = id;
                 message.obj = drawable;
                 handler2.sendMessage(message);
             }
         };
         thread.start();
         thread = null;
    }
    // 引入线程池来管理多线程
    private void loadImage3(final String url, final int id) {
        executorService.submit(new Runnable() {
            public void run() {
                try {
                    final Drawable drawable = Drawable.createFromStream(new URL(url).openStream(), "image.png");
                    handler.post(new Runnable() {

                        public void run() {
                            ((ImageView) LazyLoadImageActivity.this.findViewById(id)).setImageDrawable(drawable);
                        }
                    });
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
    //引入线程池,并引入内存缓存功能,并对外部调用封装了接口,简化调用过程
    private void loadImage4(final String url, final int id) {
          //如果缓存过就会从缓存中取出图像,ImageCallback接口中方法也不会被执行
         Drawable cacheImage = asyncImageLoader.loadDrawable(url,new AsyncImageLoader.ImageCallback() {
             //请参见实现:如果第一次加载url时下面方法会执行
             public void imageLoaded(Drawable imageDrawable) {
               ((ImageView) findViewById(id)).setImageDrawable(imageDrawable);
             }
         });
        if(cacheImage!=null){
          ((ImageView) findViewById(id)).setImageDrawable(cacheImage);
        }
    }

    //采用Handler+Thread+封装外部接口
    private void loadImage5(final String url, final int id) {
          //如果缓存过就会从缓存中取出图像,ImageCallback接口中方法也不会被执行
         Drawable cacheImage = asyncImageLoader3.loadDrawable(url,new AsyncImageLoader3.ImageCallback() {
             //请参见实现:如果第一次加载url时下面方法会执行
             public void imageLoaded(Drawable imageDrawable) {
               ((ImageView) findViewById(id)).setImageDrawable(imageDrawable);
             }
         });
        if(cacheImage!=null){
                    ((ImageView) findViewById(id)).setImageDrawable(cacheImage);
        }
    }


}




XML:

<?xml version="1.0" encoding="utf-8"?>  
  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
              android:layout_width="fill_parent"  
              android:orientation="vertical"  
              android:layout_height="fill_parent" >  
  <ImageView android:id="@+id/image1" android:layout_height="wrap_content" android:layout_width="fill_parent"></ImageView>  
   <ImageView android:id="@+id/image2" android:layout_height="wrap_content" android:layout_width="fill_parent"></ImageView>  
    <ImageView android:id="@+id/image3" android:layout_height="wrap_content" android:layout_width="fill_parent"></ImageView>  
    <ImageView android:id="@+id/image5" android:layout_height="wrap_content" android:layout_width="fill_parent"></ImageView>  
    <ImageView android:id="@+id/image4" android:layout_height="wrap_content" android:layout_width="fill_parent"></ImageView>  
</LinearLayout>  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值