Picasso framework for getting started with Android

1、 Introduction:

Picasso is an Android graphics cache library open source by square. You can download and cache pictures.

2、 Picasso characteristics

Picasso is an Android image loading and caching framework, which has the following features:

1. Support task priority, and the pictures with higher "priority" will be loaded first.

2. With statistical monitoring function, you can count the cache hit rate, monitor the used memory in real time, etc.

3. It can automatically adjust the number of concurrent threads according to the current network state.

4. Support delayed loading of pictures.

5. It does not have local cache, but uses okhttp implementation.

In addition to its simple use and rich features, Picasso also has the advantage that the source code is clear and easy to read, which is suitable for reading and learning.

3、 Basic use of Picasso

1. Add dependency

Before using Picasso, we need to add dependencies first. If gradle is used, we only need to add dependencies in the build Add the following statement to gradle:

Of course, you can also download the jar package and add it to the project, which will not be repeated here.

2. Display pictures

It's really too easy to do this with Picasso. Just use the following sentence:

Where context is the current application context and ImageView is the ImageView object in which we want to display the picture. Picasso will not only display the specified pictures in our specified ImageView, but also help us do the following things (very considerate and wooden):

1. Detect the reuse of view in the adapter and automatically cancel the previous download.

2. For some complex image transformations, only small memory is required.

3. Automatic memory cache and disk cache. (1) Automatically cancel picture download

When Picasso detects that convertview is not empty (reuse of view), it will automatically cancel the previous download task of convertview.

(2) Picture transformation using Picasso, we can easily transform pictures to reduce memory consumption or be more suitable for layout. You only need a chained call like the following (it's very simple to have wood):

Of course, we can also perform more complex transformations. By implementing the transformation interface in Picasso, we can customize a "Picture Converter". The example code is as follows:

Pass the instance of the above class to the transform method to complete the custom transformation of the picture.

(3) Placeholders

Picasso supports displaying a "placeholder picture" during picture download and when picture loading fails. This feature is also very simple to use. Please see the following example code:

When the picture cannot be loaded successfully after three retries, the picture specified in the error method parameter will be displayed.

(4) Resource loading

When using Picasso, resources, assets, file system and ContentProvider can all be used as the source of pictures (it's convenient to have wood):

4、 Example demonstration

The following implements a listview to display network pictures:

Here are four pictures of online recipes, and a new dish category is added to the code:

In the main interface class, the load method is called to load the network picture, and the into method is called to set the picture to ImageView and other components.

5、 Summary

The above is the whole content of this article. See here, have you fallen in love with Picasso? Go and have fun with it. I hope this article can be helpful to your study and work.

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
分享
二维码
< <上一篇
下一篇>>