Android 如何使用Gallery设置进度条(使用LazyLoading)?

Android 如何使用Gallery设置进度条(使用LazyLoading)?,android,progress-bar,gallery,lazy-loading,Android,Progress Bar,Gallery,Lazy Loading,我正在项目中使用Fedors LazyLoad 我想做的不是在图库中显示占位符图像,我想知道是否有办法更改此代码,以便在每次加载图像时显示ProgressBar public class ImageLoader { MemoryCache memoryCache=new MemoryCache(); FileCache fileCache; private Map<ImageView, String> imageViews=Collections.synchronizedMap(n

我正在项目中使用Fedors LazyLoad

我想做的不是在图库中显示占位符图像,我想知道是否有办法更改此代码,以便在每次加载图像时显示ProgressBar

public class ImageLoader {

MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());

public ImageLoader(Context context){
    //Make the background thead low priority. This way it will not affect the UI performance
    photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);

    fileCache=new FileCache(context);
}
// Getting reference to the stub picture
final int stub_id=R.drawable.stub;
public void DisplayImage(String url, Activity activity, ImageView imageView)
{
    imageViews.put(imageView, url);
    Bitmap bitmap=memoryCache.get(url);
    if(bitmap!=null){
        imageView.setImageBitmap(bitmap);
    Log.e(url, " Was in cache");
    }
    else
    {
         Log.e(url, " Was NOT in cache");
        queuePhoto(url, activity, imageView);
//If images arent in cache i set the stub, instead i would like to set a ProgressBar.
        imageView.setImageResource(stub_id);
    }    
公共类ImageLoader{
MemoryCache MemoryCache=新的MemoryCache();
文件缓存文件缓存;
private Map ImageView=Collections.synchronizedMap(新的WeakHashMap());
公共图像加载器(上下文){
//将后台设为低优先级。这样不会影响UI性能
photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
fileCache=新的fileCache(上下文);
}
//获取对存根图片的引用
最终int stub_id=R.drawable.stub;
public void DisplayImage(字符串url、活动活动、图像视图)
{
put(imageView,url);
位图位图=memoryCache.get(url);
if(位图!=null){
设置图像位图(位图);
Log.e(url,“在缓存中”);
}
其他的
{
Log.e(url,“不在缓存中”);
队列照片(url、活动、图像视图);
//如果图像不在缓存中,我将设置存根,而不是设置进度条。
setImageResource(存根id);
}    

注意:此代码不可编译,这只是给您一个想法

  • Make
    displayImage()
    返回一些值,例如指示缓存中的图像数

  • 我猜您是在
    getView()中使用它的

  • 并修改ImageLoader

              class PhotosLoader extends Thread {
                @Override
                public void run() {
                    try {
                        while (true) {
                            //thread waits until there are any images to load in the queue
                            if (photosQueue.photosToLoad.size() == 0)
                                synchronized (photosQueue.photosToLoad) {
                                    photosQueue.photosToLoad.wait();
                                }
                            if (photosQueue.photosToLoad.size() != 0) {
                                PhotoToLoad photoToLoad;
                                synchronized (photosQueue.photosToLoad) {
                                    photoToLoad = photosQueue.photosToLoad
                                            .pop();
                                }
                                Bitmap bmp = getBitmap(photoToLoad.url);
                                cache.put(photoToLoad.url, bmp);
                                if (((String) photoToLoad.imageView.getTag())
                                        .equals(photoToLoad.url)) {
                                    BitmapDisplayer bd = new BitmapDisplayer(
                                            bmp, photoToLoad.imageView);
                                    Activity a = (Activity) photoToLoad.imageView
                                            .getContext();
                                    a.runOnUiThread(bd);
                                    Message message = new Message();   
                                    message.what = TestHandler.REMOVE_PROGRESS_BAR;       
                                    a.myHandler.sendMessage(message);    
                                }
    
                            }
                            if (Thread.interrupted())
                                break;
                        }
                    } catch (InterruptedException e) {
                        //allow thread to exit
                    }
                }
            }
    
  • 在您的活动中

     Handler myHandler = new Handler() { 
    
     ProgressDialog dialog = null; 
     public void handleMessage(Message msg) {   
     switch (msg.what) {   
     case TestHandler.SHOW_THE_DAMN_SPINNER_PROGRESS:   
              dialog  = ProgressDialog.show(this, "Working..", "Reloading cache", true,
                            false);
    
      break;   
    
        case TestHandler.REMOVE_PROGRESS_BAR: 
        dialog.dismiss();
        break;  
               }   
        super.handleMessage(msg);   
          }   
     };
    
  •  Handler myHandler = new Handler() { 
    
     ProgressDialog dialog = null; 
     public void handleMessage(Message msg) {   
     switch (msg.what) {   
     case TestHandler.SHOW_THE_DAMN_SPINNER_PROGRESS:   
              dialog  = ProgressDialog.show(this, "Working..", "Reloading cache", true,
                            false);
    
      break;   
    
        case TestHandler.REMOVE_PROGRESS_BAR: 
        dialog.dismiss();
        break;  
               }   
        super.handleMessage(msg);   
          }   
     };