Android的图像加载器 [英] Android Image loader

查看:124
本文介绍了Android的图像加载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我的图像加载它需要几秒钟加载图像,在那个时候,我需要在图像视图的中间显示进度条,请帮助我

请参见code

在我的班级我打电话图像加载器这样

 公共ImageLoader的ImageLoader的=新ImageLoader的(本);
    imageLoader.DisplayImage1(的ImagePath,image_view);   公共类ImageLoader的{的MemoryCache的MemoryCache =新的MemoryCache();
FileCache fileCache;
私人地图< ImageView的,字符串> imageViews = Collections.synchronizedMap(新的WeakHashMap< ImageView的,字符串>());
ExecutorService的ExecutorService的;
私人INT stub_id = R.drawable.head_icon;
公共ImageLoader的(上下文的背景下){
    fileCache =新FileCache(背景);
    ExecutorService的= Executors.newFixedThreadPool(5);
}
公共无效DisplayImage(字符串URL,ImageView的ImageView的)
{
    imageViews.put(ImageView的,URL);
    位图位图= memoryCache.get(URL);
    如果(位图!= NULL)
        imageView.setImageBitmap((getRoundedCornerImage(位图)));
    其他
    {
        queuePhoto(URL,ImageView的);
        imageView.setImageResource(stub_id);
    }
}
公共无效DisplayImage1(字符串URL,ImageView的ImageView的)
{
    imageViews.put(ImageView的,URL);
    位图位图= memoryCache.get(URL);
    如果(位图!= NULL)
        imageView.setImageBitmap(((位图)));
    其他
    {
        queuePhoto(URL,ImageView的);
        imageView.setImageResource(stub_id);
    }
}私人无效queuePhoto(字符串URL,ImageView的ImageView的)
{
    PhotoToLoad P =新PhotoToLoad(URL,ImageView的);
    executorService.submit(新PhotosLoader(P));
}私人位图getBitmap(字符串URL)
{
    文件F = fileCache.getFile(URL);    //从SD缓存
    位图B =去codeFILE(F);
    如果(B!= NULL)
        返回b;    //从网页
    尝试{
        位图位图= NULL;
        URL =图片网址新的URL(网址);
        HttpURLConnection的康恩=(HttpURLConnection类)imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(真);
        InputStream为= conn.getInputStream();
        OutputStream的OS =新的FileOutputStream(F);
        Utils.CopyStream(是,OS);
        os.close();
        位=去codeFILE(F);
        返回位图;
    }赶上(例外前){
       ex.printStackTrace();
       返回null;
    }
}//德$ C $连拍影像的鳞它来减少内存消耗
私人位图德codeFILE(文件f){
    尝试{
        //德code图像尺寸
        BitmapFactory.Options O =新BitmapFactory.Options();
        o.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeStream(新的FileInputStream(F),空,O);        //找到正确的比例值。它应该是2的幂。
        最终诠释REQUIRED_SIZE = 70;
        INT width_tmp = o.outWidth,height_tmp = o.outHeight;
        int标= 1;
        而(真){
            如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2版; REQUIRED_SIZE)
                打破;
            width_tmp / = 2;
            height_tmp / = 2;
            规模* = 2;
        }        //德code。与inSampleSize
        BitmapFactory.Options O2 =新BitmapFactory.Options();
        o2.inSampleSize =规模;
        返回BitmapFactory.de codeStream(新的FileInputStream(F),空,O2);
    }赶上(FileNotFoundException异常五){}
    返回null;
}//任务队列
私有类PhotoToLoad
{
    公共字符串的URL;
    公共ImageView的ImageView的;
    公共PhotoToLoad(字符串U,ImageView的我){
        URL = U;
        ImageView的= I;
    }
}类PhotosLoader实现Runnable {
    PhotoToLoad photoToLoad;
    PhotosLoader(PhotoToLoad photoToLoad){
        this.photoToLoad = photoToLoad;
    }    @覆盖
    公共无效的run(){
        如果(imageViewReused(photoToLoad))
            返回;
        BMP位图= getBitmap(photoToLoad.url);
        memoryCache.put(photoToLoad.url,BMP);
        如果(imageViewReused(photoToLoad))
            返回;
        BitmapDisplayer BD =新BitmapDisplayer(BMP,photoToLoad);
        活动一=(活动)photoToLoad.imageView.getContext();
        a.runOnUiThread(BD);
    }
}布尔imageViewReused(PhotoToLoad photoToLoad){
    字符串标记= imageViews.get(photoToLoad.imageView);
    如果(标记== NULL ||!tag.equals(photoToLoad.url))
        返回true;
    返回false;
}//用在UI线程来显示位图
类BitmapDisplayer实现Runnable
{
    位图位图;
    PhotoToLoad photoToLoad;
    公共BitmapDisplayer(位图B,PhotoToLoad P){位= B; photoToLoad = P;}
    @覆盖
    公共无效的run()
    {
        如果(imageViewReused(photoToLoad))
            返回;
        如果(位图!= NULL)
            photoToLoad.imageView.setImageBitmap(((位图)));
        其他
            photoToLoad.imageView.setImageResource(stub_id);
    }
}公共无效clearCache(){
    memoryCache.clear();
    fileCache.clear();
}  }


解决方案

不要推倒重来。使用 AQuery 例如:

 字符串图片网址=htt​​p://farm6.static.flickr.com/5035/5802797131_a729dac808_b.jpg;aq.id(R.id.image).progress(R.id.progress)在图像配(图片网址,假的,假的);

HTTP://$c$c.google.com/ p / Android的查询/维基/ ImageLoading

Pls help in my image loader it take few sec to load the image ,on that time i need to display the progress bar on middle of the image view ,pls help me

Pls see the code

in my class i'm calling image loader as this way

    public ImageLoader imageLoader = new ImageLoader(this);     
    imageLoader.DisplayImage1(imagepath, image_view); 





   public class ImageLoader {

MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService; 
private int stub_id=R.drawable.head_icon;
public ImageLoader(Context context){
    fileCache=new FileCache(context);
    executorService=Executors.newFixedThreadPool(5);
}


public void DisplayImage(String url, ImageView imageView)
{
    imageViews.put(imageView, url);
    Bitmap bitmap=memoryCache.get(url);
    if(bitmap!=null)
        imageView.setImageBitmap((getRoundedCornerImage(bitmap)));
    else
    {
        queuePhoto(url, imageView);
        imageView.setImageResource(stub_id);
    }
}


public void DisplayImage1(String url, ImageView imageView)
{
    imageViews.put(imageView, url);
    Bitmap bitmap=memoryCache.get(url);
    if(bitmap!=null)
        imageView.setImageBitmap(((bitmap)));
    else
    {
        queuePhoto(url, imageView);
        imageView.setImageResource(stub_id);
    }
}



private void queuePhoto(String url, ImageView imageView)
{
    PhotoToLoad p=new PhotoToLoad(url, imageView);
    executorService.submit(new PhotosLoader(p));
}

private Bitmap getBitmap(String url) 
{
    File f=fileCache.getFile(url);

    //from SD cache
    Bitmap b = decodeFile(f);
    if(b!=null)
        return b;

    //from web
    try {
        Bitmap bitmap=null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is=conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Exception ex){
       ex.printStackTrace();
       return null;
    }
}

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=70;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

//Task for the queue
private class PhotoToLoad
{
    public String url;
    public ImageView imageView;
    public PhotoToLoad(String u, ImageView i){
        url=u; 
        imageView=i;
    }
}

class PhotosLoader implements Runnable {
    PhotoToLoad photoToLoad;
    PhotosLoader(PhotoToLoad photoToLoad){
        this.photoToLoad=photoToLoad;
    }

    @Override
    public void run() {
        if(imageViewReused(photoToLoad))
            return;
        Bitmap bmp=getBitmap(photoToLoad.url);
        memoryCache.put(photoToLoad.url, bmp);
        if(imageViewReused(photoToLoad))
            return;
        BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
        Activity a=(Activity)photoToLoad.imageView.getContext();
        a.runOnUiThread(bd);
    }
}

boolean imageViewReused(PhotoToLoad photoToLoad){
    String tag=imageViews.get(photoToLoad.imageView);
    if(tag==null || !tag.equals(photoToLoad.url))
        return true;
    return false;
}

//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable
{
    Bitmap bitmap;
    PhotoToLoad photoToLoad;
    public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
    @Override
    public void run()
    {
        if(imageViewReused(photoToLoad))
            return;
        if(bitmap!=null)
            photoToLoad.imageView.setImageBitmap(((bitmap)));
        else
            photoToLoad.imageView.setImageResource(stub_id);
    }
}

public void clearCache() {
    memoryCache.clear();
    fileCache.clear();
}



  }

解决方案

Don't reinvent the wheel. Use AQuery for example:

String imageUrl = "http://farm6.static.flickr.com/5035/5802797131_a729dac808_b.jpg";    

aq.id(R.id.image).progress(R.id.progress).image(imageUrl, false, false);

http://code.google.com/p/android-query/wiki/ImageLoading

这篇关于Android的图像加载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆