如何设置带有图库的进度条(使用延迟加载)?

时间:2011-10-27 23:01:16

标签: 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(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);
    }    

1 个答案:

答案 0 :(得分:1)

注意:此代码无法编译,这只是为了给您一个想法,

  1. displayImage()返回一些值,例如指示缓存中的图片数量

  2. 我猜您在getView()

    中使用它
    public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if(convertView == null){
        vi = inflater.inflate(R.layout.galitem, null);
    ImageView image=(ImageView)vi.findViewById(R.id.galimage);
    int queueLength = imageLoader.DisplayImage(data[position], activity, image);
    if(queueLength > threshold) 
    {
        Message message = new Message();   
        message.what = TestHandler.SHOW_THE_DAMN_SPINNER_PROGRESS;       
        activity.myHandler.sendMessage(message);             
    }  
    }
    
    return vi;    
    }
    
  3. 修改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
                    }
                }
            }
    
    1. 在您的活动中,

       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);   
            }   
       };