1. Introduction:
Picasso is an Android graphics cache library open source by Square. It can realize image download and cache functions.
2. Characteristics of Picasso
Picasso is an Android image loading cache framework, which has the following features:
1. Support task priority and pictures with higher "priority" will be loaded first.
2. With statistical monitoring function, it can count cache hit rate, monitor used memory in real time, etc.
3. The number of concurrent threads can be automatically adjusted according to the current network status.
4. Supports delayed loading of pictures.
5. It does not have local cache itself, but uses OkHttp implementation.
In addition to being simple in use and rich in functional features, Picasso also has a major advantage that its source code is clear and easy to read, which is suitable for reading and learning.
3. Basic use of Picasso
1. Add dependencies
Before using Picasso, we first need to add dependencies. If you are using Gradle, you only need to use the module.Add the following statement:
compile ':picasso:2.5.2'
Of course, you can also download the jar package and add it to the project, so I won’t go into details here.
2. Show pictures
Using Picasso to do this is really easy, you just need the following sentence:
(context).load("/").into(imageView);
in,context
For the current application context,imageView
For the one we want to display the picture in itImageView
Object. Picasso will not only be in our designatedImageView
The specified image is displayed, and it will also help us do the following things (very considerate or not):
1. InAdapter
In-depth detectionView
reuse and automatically cancel previous downloads.
2. For some complex image transformations, only relatively small memory is required.
3. Automatic memory cache and disk cache.
(1) Automatically cancel the image download
When Picasso detectsconvertView
When not empty (View reuse), it will automatically cancel beforeconvertView
download task.
@Override public voidgetView(intposition,View convertView,ViewGroup parent) { SquaredImageView view = (SquaredImageView) convertView; if(view ==null) { view =newSquaredImageView(context); } String url = getItem(position); (context).load(url).into(view); }
(2) Picture transformation
With Picasso, we can easily transform images to reduce memory usage or be more suitable for layout. Just need a chain call like the following (very simple or not):
(context) .load(url) .resize(50,50) .centerCrop() .into(imageView);
Of course, we can also perform more complex transformations by implementing theTransformation
Interface, we can customize a "picture converter". The sample code is as follows:
public class CropSquareTransformation implements Transformation { @Override public Bitmaptransform(Bitmap source) { int size = ((),()); int x = (() - size) /2; int y = (() - size) /2; Bitmap result = (source,x,y,size,size); if(result != source) { (); } return result; } @Override public String key() { return "square()"; } }
Pass the above instance totransform
Method to complete custom transformation of the picture.
(3) Place holders
Picasso supports displaying a "placeholder picture" during image download and when the image load fails. This feature is also very simple to use. Please see the following sample code:
(context) .load(url) .placeholder(.user_placeholder) .error(.user_placeholder_error) .into(imageView);
When retrying three times, the image cannot be loaded successfully, it will be displayederror
The picture specified in the method parameters.
(4) Resource loading
When using Picasso, Resources, assets, file system, and ContentProvider can all be used as the source of the picture (it's very convenient or not):
(context).load(.landing_screen).into(imageView1); (context).load("file:///android_asset/").into(imageView2); (context).load(newFile(...)).into(imageView3);
4. Example demonstration
Implement one belowListView
Show network pictures:
I found four pictures of online recipes here and added a new dish to the code:
//Denote vegetables (cooked vegetables, eggs, meat, etc.)public class Dish { private String imgUrl; // Image address private String name; // Dish name private String price; // Vegetable price public Dish(String imgUrl, String name, String price) { = imgUrl; = name; = price; } public String getImgUrl() { return imgUrl; } public void setImgUrl(String imgUrl) { = imgUrl; } public String getName() { return name; } public void setName(String name) { = name; } public String getPrice() { return price; } public void setPrice(String price) { = price; } }
In the main interface class, callload
Method loads network image and callsinto
Method to set the image toImageView
Components like:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { private static final String BASE_URL = "http://img1./img2011/w1/106/85/"; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); ArrayList<Dish> dishList = new ArrayList<Dish>(); (new Dish(BASE_URL + "", "Boiled fish slices", "38.00")); (new Dish(BASE_URL + "", "Fried meat", "18.00")); (new Dish(BASE_URL + "", "Stir-fried vegetables", "15.00")); (new Dish(BASE_URL + "", "Gold medal roast duck", "36.00")); (new Dish(BASE_URL + "", "Vermicelli meat pot", "20.00")); ListView mListView = (ListView) (); MainListViewAdapter adapter = new MainListViewAdapter(dishList); (adapter); } // ListView adapter private class MainListViewAdapter extends BaseAdapter { private ArrayList<Dish> dishList; public MainListViewAdapter(ArrayList<Dish> list) { = list; } @Override public int getCount() { return (); } @Override public Object getItem(int position) { return (position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ListViewItemHolder item = null; if (convertView == null) { convertView = ().inflate( .main_listview_item, null); item = new ListViewItemHolder(); item.img_iv = (ImageView) convertView .findViewById(.imageView1); item.name_textview = (TextView) convertView .findViewById(.textView1); item.price_textview = (TextView) convertView .findViewById(.textView2); (item); } else { item = (ListViewItemHolder) (); } Dish dish = (position); //This is the place where network pictures are loaded asynchronously ().load(()) .into(item.img_iv); item.name_textview.setText(()); item.price_textview.setText(() + "Yuan"); return convertView; } } // ListView's Item component class private class ListViewItemHolder { ImageView img_iv; TextView name_textview; TextView price_textview; } }
5. Summary
The above is the entire content of this article. After reading this, have you fallen in love with Picasso? Go and have fun with it. I hope this article will be helpful to everyone's study and work.