【问题标题】:Picasso Library and GridView Images毕加索图书馆和 GridView 图像
【发布时间】:2015-04-03 05:49:40
【问题描述】:

我想使用 picasso 库构建一个在网格视图中显示图像的应用程序。图像由远程服务器检索。我应该创建一个 AsyncTask 类还是这个类由毕加索库本身处理?到目前为止,我看到的所有毕加索教程似乎都有些模糊。

谢谢,

西奥。

【问题讨论】:

  • Yes picasso 一个强大的安卓图像下载和缓存库。您不需要编写异步任务来下载图像,它们由这个库处理。如果您需要更多信息,请告诉我如何编写它,我可以在网格视图中向您展示。
  • 感谢您的回复。我会用 gridview 试试,如果我有任何问题,我会问你。

标签: android json gridview picasso


【解决方案1】:

使用毕加索库在gridview中加载图像非常简单,as demonstrated here,

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;
  }
}

【讨论】:

  • 谢谢我的朋友。保重。
猜你喜欢
  • 2020-02-22
  • 2014-10-10
  • 2014-10-29
  • 2014-10-02
  • 1970-01-01
  • 2016-04-18
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多