gpt4 book ai didi

android - picasso 图书馆和 GridView 图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:43:12 26 4
gpt4 key购买 nike

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

谢谢,

西奥。

最佳答案

在 gridview 中使用 picasso lib 加载图像非常简单,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;
}
}

关于android - picasso 图书馆和 GridView 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29426893/

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