Picasso basic usage of Android image loading tool

Today, let's learn about Picasso. The plan includes the following aspects:

Picasso advanced image loading tool Picasso source code analysis

At present, the popular image loading frameworks in the market mainly include universal imageloader, Picasso, glass and fresco.

The following is a brief introduction to these frameworks:

Universal image loader: this can be said to be a very, very classic one. I believe every app developer has used it. Unfortunately, the author has stopped the maintenance of the project, so it is not recommended.

Picasso: it is a picture loading framework produced by square company. Square must produce high-quality products. Its main characteristics are simple to use and strong scalability. It supports pictures from various sources, including network, resources, assets, files, content providers, etc. Okhttp's network framework is integrated internally, so if you use other frameworks of square company in your project, Picasso is recommended for better compatibility. At present, there are 12758 stars on GitHub.

Glass: it is an open source project of Google employees. It is based on a framework of Picasso. The code style is very similar to Picasso and adds more functions. It is very important to support GIF. Of course, its package will be larger. If your project has many usage scenarios for pictures and needs to support GIF, it is recommended. At present, there are 13636 stars on GitHub.

Fresco: it is an open source framework produced by FB. It is relatively new. Its biggest advantage is the optimization of memory occupation, which greatly reduces oom. The functions also include the functions of the above three frameworks, but it also brings an obvious disadvantage that it is too large. Therefore, it is recommended to use it on apps related to pictures, otherwise Picasso and glass will be enough. At present, there are 11983 stars on GitHub.

The above mainly gives a brief introduction to various frameworks. Since it is about Picasso, let's take a look at the functions of Picasso.

1. Provide memory and disk cache. It is enabled by default. It can be set not to cache. 2. Pictures displayed by default during picture loading. 3. Pictures displayed after picture loading failure or error. 4. Callback of picture loading success or failure. 5. Customize picture size, automatically measure ImageView size, crop and rotate pictures. 6. Convert pictures. 7. Label management, Pause and resume picture loading 8 request priority management 9 can load pictures from different sources, and network, resources, assets, files and content providers 10 have richer extended functions

These functions will be explained in detail in the following articles.

We mentioned many functions of Picasso above. Let's demonstrate these functions respectively

to configure:

Add a reference in build.gradle

1 load picture

Through the source code, we can find that the load method mainly needs the following overloads

It's super simple. There are only two ways.

2. Display the default picture placeholder during loading

Generally, it takes a long time to load pictures on the network, so an alternative picture will be displayed by default, and only resid and drawable local pictures are supported.

3. Display the wrong picture after loading failure

In order to display the wrong picture, I spelled a string after the correct address to construct a wrong address. Similarly, only local pictures are supported

4 picture filling method

4.1 fit()

This attribute will fill the entire view according to the size of the image view, regardless of the scale, which may cause the picture to stretch or shrink

4.2 centerCrop()

Reduce the picture in proportion to make it display in the middle and fill the view, which will cause incomplete picture display. It must be used together with the resize method

4.3 centerInside()

The picture can be fully displayed by scaling. However, if the picture is smaller than the view, it cannot fill the whole view. It must be used together with the resize method

4.4 onlyScaleDown()

The size of the test image used here is 1240 * 1563. If the width and height of resize is greater than the original width and height of the image, resize does not work and is displayed with the original width and height of the image.

5 cancel the transition display effect of the picture (nofade)

By default, the image will have a transition effect when it is displayed. After adding the. Nofade method, the effect can be cancelled. It is basically rarely used

6 picture rotation ()

7 cache policy

Picasso provides a cache debugging method, which can be set through the following code

The renderings are as follows

You can see that there is a blue triangle in the upper left corner of the picture, which indicates that the picture is loaded from disk. In addition, if it is red, it indicates that it is loaded from the network, and if it is green, it indicates that it is loaded from memory.

Picasso's caching process is to first check whether the picture is saved in the memory. If not, check whether the picture is saved in the disk. If not, download it from the network. After the download is successful, save it to the memory and disk respectively. What if we sometimes don't want to cache the picture or don't want to get the picture from the cache? Picasso also provided me with corresponding control methods.

The above two methods are completely equivalent, but the first writing method is officially not recommended. It is listed here for everyone to understand. What does this mean? Skip loading pictures from memory, and the pictures will not be cached in memory after downloading. That is, the logo in the upper left corner of the picture can never be green. MemoryPolicy.NO_ Cache: directly skip checking whether the picture is cached in memory memorypolicy.no_ Store: images are not cached in memory after downloading

Similarly, this method means to skip loading pictures from the disk, and the pictures will not be cached in the disk after downloading. Note here that it is not cached on disk, but will be cached in memory. Therefore, if you do not want to cache in memory and disk, you need to use the two methods together, as follows:

There is also a value offline in the networkpolicy enumeration, which means that it is forced to fetch from the cache and will not initiate network requests. If it is not in the cache, it will not be requested from the network.

8 priority

Imagine a scenario. When we open an interface, there is a list on the interface, each list item has a picture to load, and there is a picture on the list to load in advance. So how to schedule the priority of each request? Picasso provides us with a priority method to manage the priority of requests

We can know from the comment of the priority method that the default priority is normal, so we can realize high priority loading as follows:

9 tag management

Children's shoes that have used the list to load pictures know that the loading of pictures is stopped during the list scrolling process, and the loading of pictures is resumed when the scrolling is stopped. How can this function be realized in Picasso? This uses the tag function. Set the tag through the following code:

The Picasso class provides the following methods to control the tag

It can be well understood by the name. We call it when the list scrolls

Called when scrolling stops

Canceltag is used to cancel the download. Generally, we cancel the unfinished request when the activity is destroyed.

10 manually specify the key value stablekey

Let's guess a question. How does Picasso know whether there is a cached picture? It is generally judged according to the key value. How is the key value generated? By reading the source code, we can know whether the rotation angle is set, whether resize is set, or whether it is a spliced string such as centercrop or centerinside according to the incoming URI or resourceid. Here, we can use the stablekey method to replace the incoming URI or resourceid to generate a key value.

Well, that's all for this article. In the next article, we will continue to learn the more advanced usage of Picasso and realize richer functions through expansion.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>