Search code examples
androidandroid-asynctaskpicasso

Multi ImageLoaders with Picasso library in Android


I'm using Picasso Library and i have (for example) five ImageView and i need to show them with Picassoand i know we can do like this:

ImageView footer1 = (ImageView) findViewById(R.id.img_thumbnail1);
        Picasso.with(MainActivity.this).load("http://url.com/1.jpg").into(footer);

        ImageView footer2 = (ImageView) findViewById(R.id.img_thumbnail2);
        Picasso.with(MainActivity.this).load("http://url.com/2.jpg").into(footer);

        ImageView footer3 = (ImageView) findViewById(R.id.img_thumbnail3);
        Picasso.with(MainActivity.this).load("http://url.com/3.jpg").into(footer);

        ImageView footer4 = (ImageView) findViewById(R.id.img_thumbnail4);
        Picasso.with(MainActivity.this).load("http://url.com/4.jpg").into(footer);

        ImageView footer5 = (ImageView) findViewById(R.id.img_thumbnail5);
        Picasso.with(MainActivity.this).load("http://url.com/5.jpg").into(footer);

But, can we use AsyncTask for load these multiloading?

Or what is the best way for do?


Solution

  • Picasso in the above case is a singleton instance, meaning you aren't creating a new Picasso object each time you load an image. Also the Images are loaded intelligently on a background queue with a designated task dispatcher so doing what you are doing above is perfectly fine. Do it in a loop if you need to cut the code.

    You can of course use AsyncTask, but it will not make it any faster. In contrary it will bring more code complexity and overhead.

    Picasso will also automatically manage the number of images it tries to download at once depending on device connection type (3G, Wifi etc).