中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

線程池+緩存+Handler 異步加載網(wǎng)絡(luò)圖片

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
 
/**
* 線程池+緩存+Handler加載圖片
*/
public class AsyncLoadImage {
    //緩存
    private Map<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();
    //線程池
    private ExecutorService executorService = Executors.newFixedThreadPool(20);//總共有10個(gè)線程循環(huán)使用
    //Hanlder
    private Handler mHandler = new Handler();
    public interface ImageCallback {
        void imageLoad(Drawable image, String imageUrl);
    }
    /**
     *
     * @param imageUrl 圖片的地址
     * @param imageCallback 回調(diào)接口
     * @return 返回內(nèi)存中緩存的圖像 第一次返回null
     */
    public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
        Log.i("AsyncLoadImage", "loadDrawable()" + imageUrl);
        //如果緩存中有則從緩存中取出來(lái)
        if(imageCache.containsKey(imageUrl)) {
            SoftReference<Drawable> softReference = imageCache.get(imageUrl);
            if(softReference.get() != null) { //判斷是否有drawable
                return softReference.get(); //有則返回
            }
        }
        //使用線程池下載圖片
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    //                                        final Drawable drawable = Drawable.createFromStream(new URL(imageUrl).openStream(), "image.jpg");
                    final Drawable drawable = getDrawableFormUrl(imageUrl); //調(diào)用獲取數(shù)據(jù)的方法
                    imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));//將加載的圖片放入到內(nèi)存中
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            imageCallback.imageLoad(drawable, imageUrl); //接口回調(diào)
                        }
                    });
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
 
        });
        return null ;
    }
    /**
     * 從網(wǎng)絡(luò)上獲取數(shù)據(jù)
     */
    public Drawable getDrawableFormUrl(String imageUrl) {
        Drawable drawable = null ;
        try {
            drawable = Drawable.createFromStream(new URL(imageUrl).openStream(), "image.jpg");
        } catch (Exception e) {
            throw new RuntimeException();
        }
        return drawable ;
    }
 
}

標(biāo)簽: 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:JavaScript判斷瀏覽器版本

下一篇: Android多線程實(shí)現(xiàn)文件斷點(diǎn)下載