Detailed use of Picasso

Keywords: Google

Put it down first. I remember the second half of the year before last when I started contacting and using Glide.
The reason for using Glide is simple. How good are all kinds of exaggerated Glide on the Internet, such as loading pictures to save memory, loading GIF pictures, Google recommendation and so on. But now I find a strange phenomenon. A large number of people on the Internet began to say how good picasso is. For example, the memory can be very small, and the number of package decimal methods is small, which can reduce the occurrence of 64k problems.

First of all, I used glide when the problems occurred, such as cached to the local image can not be retrieved, if you want to use it again, you need to modify the original code, such as the path of the image into the collection, then manually call the collection (a year ago).
For example, the change of RAGB value of image can only be changed by the whole situation, but I often encounter the problem of switching (by default, for example, the background of the white image will be yellow and ugly) can not be very good switching, and so on.

What I said so much is to prove that I am not a drifter. .

Let's take a look at a brief summary of the picaso API

First add dependencies

compile 'com.squareup.picasso:picasso:2.5.2'

Second simple use

 Picasso.with(this).load("http://www.haha.cn/large/aaaa.jpg").into(mImageView);

First parameter context
The second parameter is the picture address or file or uri
The third parameter shows the imageview

Third API

 //Load default images
.placeholder(R.drawable.default_bg) 
//Pictures displayed when loading failed
.error(R.drawable.error_iamge) 
 //Set Picture Size
.resize(400,200)
//Read width and height from configuration file
.resizeDimen(R.dimen.image_width,R.dimen.image_height)
//Setting the size of the picture must be smaller than the size of the original picture
.onlyScaleDown()
//Image clipping and image viewing: ScalType
.centerCrop()
//Fully display the whole picture, but the size of the picture should not be smaller than the size of the image view.
.centerInside()
//Automatically measure the size of the view and call the resize method, but the width and height of the view must be fixed.
.fit()
//Rotate pictures. The last two rotates (180, 200, 100) are centers.
.rotate(180)
//Conversion and scaling mask shapes for images requires implements Transformation
transform(new Transformation(this))
//There are three priorities to set the priority of image loading. LOW, NORMAL and HIGH are not selected by default, and fetch is the lowest.
//Skip memory cache
//NO_CACHE: Indicates skipping memory cache checks when processing requests
//NO_STORE: Represents that after a successful request, the final result is not stored in memory
.memoryPolicy(MemoryPolicy.NO_CACHE,MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_CACHE)//Skip disk caching

//Add tags
tag("PhotoTag")
cancelTag(Object tag) //Cancel all requests for a given tag
pauseTag(Object tag) //Suspend all requests for a given tag
resumeTag(Object tag) //All requests for a given tag that resume is suspended

//Getting pictures is much easier than glide.
Bitmap bitmap =  Picasso.with(this).load(URL).get();

Fourth, build a local instance. If we call it directly, it's a global instance.

    Picasso.Builder builder =newPicasso.Builder(this); 
    //Construct a Picasso
    Picasso picasso = builder.build();
    //Loading pictures
    picasso.load(URL)
            .into(mImageView);

Fifth, configure the custom downloader downLoader
public class CustomDownloader implements Downloader {

@Override
public Response load(Uri uri, int networkPolicy) throws IOException {
    return null;
}

@Override
public void shutdown() {

}
}
    //Configure the downloader
    builder.downloader(new CustomDownloader());
    //Construct a Picasso
    Picasso picasso = builder.build();

Sixth Configuration Cache

    // Setting cache size
     LruCache cache = new LruCache(5*1024*1024);
    builder.memoryCache(cache);

Seventh Configuration Thread Pool

     //Configure thread pool
          ExecutorServiceexecutorService=Executors.newFixedThreadPool(8);
   builder.executor(executorService);

Eighth Configuration of Global Picasso Instance
We want these custom configurations to be applied across the project and configure only once.

setSingletonInstance

Reference resources: Photo Loading Framework - Picasso's Most Detailed Guide to Use

Posted by robviperx on Mon, 15 Apr 2019 00:06:31 -0700