gpt4 book ai didi

android - 滚动时 Gridview 内的 ImageView 重复图像(使用 ImageDownloader)

转载 作者:行者123 更新时间:2023-11-29 20:43:46 25 4
gpt4 key购买 nike

正如标题所说,我有这个问题。

因为我需要从网络检索图像,所以我实现了一个解决方案,解释为 here

我将发布 Adapter 和 ImageDownloader 代码(我已自由修改下载器以使功能更适合我的程序)。

适配器方法:

@Override
public View getView(int position, View view, ViewGroup parent) {

ViewHolder holder;

if (view == null) {
view = inflater.inflate(R.layout.grid_adapter_camera_layout, parent, false);

holder = new ViewHolder();
holder.progress = (ProgressBar) view.findViewById(R.id.progressBar);
holder.image = (ImageView) view.findViewById(R.id.camera_image);
holder.text = (TextView) view.findViewById(R.id.camera_name);
holder.text.setEllipsize(TextUtils.TruncateAt.END);
holder.text.setLines(2);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}

imgDownloader.download(values.get(position).getUrl(), holder.image, holder.progress);
}

这里是 ImageDownloader(我只发布了最合适的代码部分,如果您需要更多,请不要犹豫,索取它)。

public void download(String url, ImageView imageView, ProgressBar progress) {

Bitmap bitmap = getBitmapFromCache(url);
if (bitmap != null) {
cancelPotentialDownload(url, imageView);
imageView.setImageBitmap(bitmap);
} else if(cancelPotentialDownload(url, imageView)){
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, progress);
imageView.setTag(new WeakReference<BitmapDownloaderTask>(task));
task.execute(url);
}
}

private boolean cancelPotentialDownload(String url, ImageView imageView) {
BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);

if (bitmapDownloaderTask != null) {
String bitmapUrl = bitmapDownloaderTask.url;
if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
bitmapDownloaderTask.cancel(true);
} else {
// The same URL is already being downloaded.
return false;
}
}
return true;
}

@SuppressWarnings("unchecked")
private BitmapDownloaderTask getBitmapDownloaderTask(ImageView imageView) {
if (imageView != null && imageView.getTag() != null) {
return ((WeakReference<BitmapDownloaderTask>) imageView.getTag()).get();
}
return null;
}

public class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
String url;
WeakReference<ImageView> viewReference;
WeakReference<ProgressBar> progressReference;

public BitmapDownloaderTask(ImageView view, ProgressBar progress) {
view.setTag(false);
viewReference = new WeakReference<ImageView>(view);
progressReference = new WeakReference<ProgressBar>(progress);
}

@Override
protected Bitmap doInBackground(String... params) {
url=params[0];
Log.e("DOWNLOAD URL", url);
return downloadBitmap(params[0]);
}

@Override
protected void onPostExecute(Bitmap result) {
if (isCancelled())
result = null;

addBitmapToCache(url, result);

if (viewReference != null) {
ImageView imgView = viewReference.get();
BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imgView);

if (this == bitmapDownloaderTask) {
if (result != null)
imgView.setImageBitmap(result);
else
imgView.setImageResource(R.drawable.no_signal);
}
}

if (progressReference != null) {
ProgressBar prog = progressReference.get();
if (prog != null)
prog.setVisibility(View.GONE);
}

}

}

最佳答案

我建议你使用一个库来下载图像,picasso做得很好。另一件事看看this来自 dev-bytes 的视频。它是关于动画的,但下载图像的技巧应该是一样的。这两个步骤应该有效,我已经这样做了几次并且效果很好。

关于android - 滚动时 Gridview 内的 ImageView 重复图像(使用 ImageDownloader),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30643528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com