【发布时间】:2016-12-21 05:22:40
【问题描述】:
您好,我使用下面的代码在小部件列表视图项上加载图像。
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 下载图片。我不想每次滚动我的应用小部件列表视图时都重新加载它。第一次下载后,它应该保存在缓存中。你能建议我这样做吗?
【问题讨论】:
-
这不是您问题的解决方案,而是建议您为什么不能使用glide。在我的情况下它更好。我也希望你?
-
@Raghavendra 嗨,我正在使用小部件,我们可以像这样在图像视图上设置图像 remoteView.setImageViewBitmap(R.id.feed_image, mBitmap);其中 R.id.feed_image 是我的图像视图 ID。我在小部件中使用 glide 库时遇到了麻烦。你知道如何在这样的小部件中使用滑翔吗?
标签: android bitmap widget android-widget