毕加索库和网格视图图片

6
我想构建一个应用程序,使用Picasso库在GridView中显示图像。这些图像是通过远程服务器检索的。我应该创建一个AsyncTask类,还是这个类由Picasso库自己处理?到目前为止,我看到的所有Picasso教程都有点模糊。
谢谢, Theo.

2
Picasso是一款强大的Android图像下载和缓存库。您无需编写异步任务来下载图像,这些任务由该库处理。如果您需要更多关于如何编写它的信息,我可以通过在GridView中实现来向您展示。 - Bhavdip Sagar
谢谢回复。我会尝试使用GridView,如果遇到任何问题,我会向您请教。 - Theo
1个回答

9

使用Picasso库在GridView中加载图片非常简单,如此示范

class SampleGridViewAdapter extends BaseAdapter {
  private final Context context;
  private final List<String> urls = new ArrayList<String>();

  public SampleGridViewAdapter(Context context) {
    this.context = context;

    // Ensure we get a different ordering of images on each run.
    Collections.addAll(urls, Data.URLS);
    Collections.shuffle(urls);

    // Triple up the list.
    ArrayList<String> copy = new ArrayList<String>(urls);
    urls.addAll(copy);
    urls.addAll(copy);
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    SquaredImageView view = (SquaredImageView) convertView;
    if (view == null) {
      view = new SquaredImageView(context);
      view.setScaleType(CENTER_CROP);
    }

    // Get the image URL for the current position.
    String url = getItem(position);

    // Trigger the download of the URL asynchronously into the image view.
    Picasso.with(context) //
        .load(url) //
        .placeholder(R.drawable.placeholder) //
        .error(R.drawable.error) //
        .fit() //
        .tag(context) //
        .into(view);

    return view;
  }

  @Override public int getCount() {
    return urls.size();
  }

  @Override public String getItem(int position) {
    return urls.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }
}

谢谢你,我的朋友。保重。 - Theo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接