gpt4 book ai didi

android - 使用从互联网下载的图像延迟加载 GridView

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:50:28 25 4
gpt4 key购买 nike

我已经访问 Stack Overflow 多年了,这是我第一次找不到任何可以解决我的问题的帖子(至少我没有看到)。

我有一个带有自定义适配器的 GridView,我已覆盖它以返回由 ImageViewTextView 制作的自定义 View 。

我在 JSON 使用 AsyncTask 从 URL 解析图像后加载图像,将所有信息存储到 doInBackground() 中的 ArrayList方法并在 onPostExecute() 方法中调用 notifyDataSetChanged()。一切都很好。

现在我的问题是,当我启动 Activity 时,需要 5-10 秒的时间, GridView 才会创建并在实体中呈现给用户。我想知道是否有一种方法可以先显示带有文本信息的 GridView ,然后再加载每个图像。这是否可能,因为它们都是用相同的方法创建的?

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View v = null;
if (arg1 == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.custom_product_view, null);
} else {
v = arg1;
}

iv = (ImageView) v.findViewById(R.id.product_image);
imageLoader.DisplayImage(products.get(arg0).getImage(), iv);

TextView tv = (TextView) v.findViewById(R.id.product_price);
tv.setText(products.get(arg0).getPrice());

return v;
}

我还必须通知您,正如您从 DisplayImage() 方法中看到的那样,我已经实现了延迟加载:Lazy load of images in ListView .它工作正常,但问题是它会再次加载整个 View 。我想要做的是启动 Activity ,首先加载标题,然后在完成下载时加载图像。使用此处的代码,它只是延迟加载 GridView 的每个单元格包含的整个 View 。我赚了几秒钟,因为我没有像以前那样一次下载所有图像,但这仍然不是我要搜索的内容。

非常感谢。

最佳答案

遵循这种方法。

首先,创建一个自定义 WebImageView 类,如下所示。

public class WebImageView extends ImageView {

private Drawable placeholder, image;

public WebImageView(Context context) {
super(context);
}
public WebImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public WebImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public void setPlaceholderImage(Drawable drawable) {
placeholder = drawable;
if (image == null) {
setImageDrawable(placeholder);
}
}
public void setPlaceholderImage(int resid) {
placeholder = getResources().getDrawable(resid);
if (image == null) {
setImageDrawable(placeholder);
}
}

public void setImageUrl(String url) {
DownloadTask task = new DownloadTask();
task.execute(url);
}

private class DownloadTask extends AsyncTask<String, Void, Bitmap> {

@Override
protected Bitmap doInBackground(String... params) {
String url = params[0];
try {
URLConnection conn = (new URL(url)).openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current=bis.read()) != -1) {
baf.append((byte)current);
}

byte[] imageData = baf.toByteArray();
return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);

} catch (Exception e) {
return null;
}
}

@Override
protected void onPostExecute(Bitmap result) {
image = new BitmapDrawable(result);
if (image != null) {
setImageDrawable(image);
}
}
}
}

接下来,在Activity中使用上面的自定义ImageView如下:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

WebImageView imageView = (WebImageView) findViewById(R.id.webimage);
imageView.setPlaceholderImage(R.drawable.ic_launcher);
imageView.setImageUrl("http://www.google.co.in/images/srpr/logo3w.png");
}

简而言之,您正在为 ImageView 设置一个占位符图像,当下载完成时它会被实际图像替换。因此 GridView 将立即呈现而不会延迟。

实现细节:因此,在您的自定义 View (带有图像 + 文本)中,不要使用简单的 ImageView,而是使用如上所示的 WebImageView。当您获得 JSON 响应时,设置带有标题的 TextView 和带有图像 url 的 WebImageView。因此标题会立即显示,图片会延迟加载。

关于android - 使用从互联网下载的图像延迟加载 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15607406/

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