Picasso Image加载多线程?

时间:2016-08-03 11:23:10

标签: java android multithreading picasso

我一直在寻找一种方法来在一个单独的线程中做毕加索,因为它是从互联网上加载的。我不知道如何确认是否。

我的应用程序基本上是一个带有一个imageView和两个textView的ListView,我创建了一个CustomAdapter,它扩展了BaseAdapter以协助List。这就是我在我的适配器类中所做的:

public class CustomAdapter extends BaseAdapter
{
public String title[];
public String description[];
public String images[]; //Image URLs
private static Picasso instance;
public Activity context;
public LayoutInflater inflater;


public CustomAdapter(Activity context, String[] title, String[] description, String[] images) {
    super();

    this.context = context;
    this.title = title;
    this.description = description;
    this.images = images;

    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return title.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public static Picasso getSharedInstance(Context context)
{
    if(instance == null)
    {
        instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build();
        return instance;
    }
    else
    {
        return instance;
    }
}

public static class ViewHolder
{
    ImageView icon;
    TextView itemNameTextView;
    TextView itemDescriptionTextView;

}


@Override
public View getView(int position, View view, ViewGroup parent) {
    // TODO Auto-generated method stub

    Debug.startMethodTracing("ThreadCheck");
    ViewHolder holder;
    if(view==null)
    {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.list_item, null);

        holder.icon = (ImageView) view.findViewById(R.id.DownloadedImage);
        holder.itemNameTextView = (TextView) view.findViewById(R.id.ItemNameTextView);
        holder.itemDescriptionTextView = (TextView) view.findViewById(R.id.ItemDescriptionTextView);

        view.setTag(holder);
    }
    else
        holder=(ViewHolder)view.getTag();


    Picasso.with(context).load(images[position]).into(holder.icon);

    holder.itemNameTextView.setText(title[position]);
    holder.itemDescriptionTextView.setText(description[position]);

    return view;
}

}

我想知道的是:

1)以下是否使Picasso以单独的线程加载图像? (我在某个地方读过这篇文章,对我来说并没有多大意义,我希望你们能向我解释一下。)

    instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build();

2)或者

Picasso.with(context).load(images[position]).into(holder.icon);

自动在新线程中执行吗?

3)或者这两个都没有在一个新的线程中进行,我需要单独进行吗?如果是,那怎么回事?

提前致谢!

1 个答案:

答案 0 :(得分:0)

默认情况下,

Picasso在后​​台线程中从Internet下载图像。这就是我们使用这些库进行图像加载的原因。