我在项目中使用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);
}
答案 0 :(得分:1)
注意:此代码无法编译,这只是为了给您一个想法,
让displayImage()
返回一些值,例如指示缓存中的图片数量
我猜您在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;
}
修改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);
}
};