毕加索图书馆和GridView图像 [英] Picasso Library and GridView Images

查看:106
本文介绍了毕加索图书馆和GridView图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用picasso库构建一个在gridview中显示图像的应用程序。图像由远程服务器检索。我应该制作一个AsyncTask类还是该类由毕加索图书馆本身处理?到目前为止,我所见过的所有毕加索教程都显得有些模糊。



谢谢,

Theo。

解决方案

使用picasso lib在gridview中加载图像非常简单,正如这里展示的

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

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

//确保我们在每次运行中获得不同的图像排序。
Collections.addAll(网址,Data.URLS);
Collections.shuffle(网址);

//将列表重新排列。
ArrayList< String> copy = new ArrayList< String>(urls);
urls.addAll(copy);
urls.addAll(copy);

$ b @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);
}

//获取当前位置的图片网址。
String url = getItem(position);

//将URL的异步下载触发到图像视图中。
Picasso.with(context)//
.load(url)//
.placeholder(R.drawable.placeholder)//
.error(R.drawable.error )//
.fit()//
.tag(context)//
.into(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;
}
}


I want to build an app that displays images in a gridview by using the picasso library. The images are retrieved by a remote server. Should I make an AsyncTask class or is this class handled by Picasso Library itself? All the picasso tutorials I have seen so far seem a bit vague.

Thanks,

Theo.

解决方案

Its very simple to use picasso lib for loading images in 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;
  }
}

这篇关于毕加索图书馆和GridView图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆