您好我使用下面的代码在widget listview项目上加载图片。
Map<Integer, Boolean> flags = Collections.synchronizedMap(new HashMap<Integer, Boolean>());
Bitmap mBitmap;
Handler handler = new Handler(Looper.getMainLooper());
flags.put(position, false);
handler.post(new Runnable() {
@Override
public void run() {
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.build();
imageLoader.loadImage(item.mTooteet.getThumbUrl1() + "&userToken=" + userPreference.getUserToken(), options,
new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
Log.d(TAG,"onLoadingStarted "+position);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
Log.d(TAG,"onLoadingFailed "+position);
flags.put(position, true);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
Log.d(TAG,"onLoadingCancelled "+position);
flags.put(position, true);
}
@Override
public void onLoadingComplete(String arg0, View arg1, Bitmap bitmap) {
Log.d(TAG,"onLoadingComplete "+position);
mBitmap = bitmap;
flags.put(position, true);
}
});
}
});
while (!flags.get(position)) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flags.put(position, false);
if (mBitmap != null) {
Log.d(TAG,"onLoadingComplete mBitmap not null "+position);
remoteView.setImageViewBitmap(R.id.feed_image, mBitmap);
} else {
Log.d(TAG,"onLoadingComplete mBitmap null "+position);
remoteView.setImageViewResource(R.id.feed_image, R.drawable.app_icon);
}
mBitmap = null;
在我的应用程序文件中,我使用了以下代码,
// UNIVERSAL IMAGE LOADER SETUP
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisc(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration uilConfig = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.discCacheSize(100 * 1024 * 1024).build();
ImageLoader.getInstance().init(uilConfig);
// END - UNIVERSAL IMAGE LOADER SETUP
但是这段代码每次都会从url下载图片。每次滚动我的应用程序小部件列表视图时,我都不想重新加载它。首次下载后应将其保留在缓存中。你能建议我这么做吗?
答案 0 :(得分:0)
从您的代码中,您每次都在创建DisplayImageOptions。我认为这是制作DisplayImageOptions一次的原因(在你的Runnable对象之外) 使用方式如下:
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.build()
然后在runnable中运行其他代码
handler.post(new Runnable() {
@Override
public void run() {
imageLoader.loadImage(..........)///same code you written
}
});
请让我知道结果。