android非同步載入圖片

來源:互聯網
上載者:User

package cn.com.mzba.service;

import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;債務追討
/**
 * 實現圖片的非同步載入顯示
 * @author kandanle
 *
 */
public class AsyncImageLoader {

    /**
     * 軟引用對象,在響應記憶體需要時,由記憶體回收行程決定是否清除此對象。軟引用對象最常用於實現記憶體敏感的緩衝。
     */
    private HashMap<String, SoftReference<Drawable>> imageCache;
   
    public AsyncImageLoader(){
        imageCache = new HashMap<String, SoftReference<Drawable>>();
    }
   
    public Drawable loadDrawable(final String imageUrl,final ImageView imageView,final ImageCallback imagecallback){
        if(imageCache.containsKey(imageUrl)){
            //從緩衝中讀取人人
            SoftReference<Drawable> softReference = imageCache.get(imageUrl);
            Drawable drawable = softReference.get();
            if(drawable != null){
                return drawable;
            }
        }
        final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                imagecallback.imageLoaded((Drawable)msg.obj, imageView, imageUrl);
            }
        };
        new Thread(){
            public void run() {
                Drawable drawable = loadImageFromUrl(imageUrl);
                imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
                Message message = handler.obtainMessage(0, drawable);
                handler.sendMessage(message);
            }
        }.start();
       
        return null;
    }
   
    public static Drawable loadImageFromUrl(String urlPath){
        URL url;
        InputStream is = null;
        try{
            url = new URL(urlPath);
            is = (InputStream)url.getContent();
        }catch(Exception e){
            e.printStackTrace();
        }
        Drawable drawable = Drawable.createFromStream(is, "src");
        return drawable;
    }
   
}

package cn.com.mzba.service;

import android.graphics.drawable.Drawable;
import android.widget.ImageView;

public interface ImageCallback {
    public void imageLoaded(Drawable imageDrawable,ImageView imageView, String imageUrl);
}

Drawable drawable = asyncImageLoader.loadDrawable(weiboInfo.getUserIcon(), viewUserIcon, new ImageCallback() {
    @Override
    public void imageLoaded(Drawable imageDrawable, ImageView imageView,String imageUrl) {
    imageView.setImageDrawable(imageDrawable);
        }
});
if(drawable == null){風之境地 java-javascript 蘑菇街女裝
    viewUserIcon.setImageResource(R.drawable.portrait);
}else{
    viewUserIcon.setImageDrawable(drawable);
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.