First, Introduction:
Picasso is an open-source Android graphics cache for the square company. can implement picture downloading and caching function.
Ii. Characteristics of Picasso
Picasso is an Android image load caching framework that has the following characteristics:
1. Support task priority, will first load "priority" higher picture.
2. With statistical monitoring functions, you can count the cache hit rate, real-time monitoring of the memory used and so on.
3. Ability to automatically adjust the number of concurrent threads based on current network status.
4. Supports delayed loading of pictures.
5. It does not have a local cache, but instead uses a okhttp implementation.
Picasso in addition to the use of relatively simple, rich features, but also have a major advantage is that the source code is clear and easy to read, suitable for reading and learning.
Iii. Basic use of Picasso
1. Adding dependencies
Before using Picasso, we need to add dependencies first, and if we're using gradle, just add the following statement to the module build.gradle :
Compile ' com.squareup.picasso:picasso:2.5.2 '
Of course, you can download the jar package and add it to the project, which is no longer discussed here.
2. Display Picture
Use Picasso to do this is really simple to do not want to, only need the following sentence can be:
Picasso.with. Load ("Yun_qi_img/dvpvklr.jpg"). into (ImageView);
Where, context for the current application context, the imageView object that we want to display the picture in ImageView . Picasso will not only display the specified picture in our designation ImageView , but will also help us to do the following things (very intimate with wood):
1. Reuse in the Adapter detection View and automatically cancel the download before.
2. For some complex picture transformations, it only needs to be smaller than the memory.
3. Automatic memory caching and disk caching.
(1) Automatically cancel picture download
When Picasso detects that convertView it is not empty (the view is reused), it automatically cancels the previous convertView download task.
@Override public
Voidgetview (Intposition,view convertview,viewgroup parent) {
Squaredimageview View = ( Squaredimageview) Convertview;
if (view ==null) {
view =newsquaredimageview (context);
}
String url = getitem (position);
Picasso.with. Load (URL). into (view);
}
(2) Picture transformation
with Picasso, we can easily transform the image to reduce memory footprint or to fit the layout. Just want a chained call like the one below (very simple with wood):
Picasso.with.
load (URL)
. Resize (50,50). Centercrop (
). into
(ImageView);
Of course, we can also make more complex transformations, by implementing the interface in Picasso Transformation , we can customize a "picture converter". The sample code is as follows:
public class Cropsquaretransformation implements transformation {
@Override public
bitmaptransform (Bitmap SOURCE) {
int size = Math.min (Source.getwidth (), Source.getheight ());
int x = (Source.getwidth ()-size)/2;
int y = (source.getheight ()-size)/2;
Bitmap result = Bitmap.createbitmap (source,x,y,size,size);
If (result!= source) {
source.recycle ();
}
return result;
}
@Override Public
String key () {return
"square ()";
}
}
Pass the instance of the above class to the transform method to complete the custom transformation of the picture.
(3) placeholder (place holders)
Picasso supports displaying a "placeholder picture" during picture downloading and when a picture fails to load, and this feature is simple to use, see the following sample code:
Picasso.with (context).
load (URL)
. Placeholder (r.drawable.user_placeholder).
Error ( R.drawable.user_placeholder_error)
. into (ImageView);
errorthe picture specified in the method argument is displayed when you retry the picture three times and still cannot load it successfully.
(4) Resource loading
In the use of Picasso, resources, assets, file systems, ContentProvider can be used as the source of pictures (very convenient to have wood):
Picasso.with. Load (R.drawable.landing_screen). into (imageView1);
Picasso.with. Load ("File:///android_asset/DvpvklR.png"). into (IMAGEVIEW2);
Picasso.with (context). Load (NewFile (...)). Into (IMAGEVIEW3);
Iv. Example Demo
The following implementation of a ListView display network picture:
Here are four web recipe pictures, add a new menu in your code:
Represents the public
class Dish {
private string imgurl;//Picture address
private string name;//The name
of the dish (cooked vegetables, egg, meat, etc.) Ate String price; The menu price
is public Dish (string imgurl, string name, string price) {
This.imgurl = Imgurl;
this.name = name;
This.price = Price;
}
Public String Getimgurl () {return
imgurl;
}
public void Setimgurl (String imgurl) {
this.imgurl = Imgurl;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public String GetPrice () {return price
;
}
public void Setprice (String price) {
This.price = Price;
}
In the main interface class, the calling load method loads a network picture, calling the into method to set the picture to ImageView such a component:
Import java.util.ArrayList;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import Com.squareup.picasso.Picasso; public class Mainactivity extends activity {private static final String Base_url = "Http://img1.3lian.com/img2011/w1
/106/85/";
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
arraylist<dish> dishlist = new arraylist<dish> ();
Dishlist.add (New Dish (Base_url + "42.jpg", "Boiled fish fillet", "38.00"));
Dishlist.add (New Dish (Base_url + "34.jpg", "Stir-fry meat", "18.00"));
Dishlist.add (New Dish (Base_url + "37.jpg", "stir fry vegetables", "15.00"));
Dishlist.add (New Dish (Base_url + "11.jpg", "Gold roast Duck", "36.00")); DiShlist.add (New Dish (Base_url + "12.jpg", "fan meat pot", "20.00"));
ListView Mlistview = (ListView) This.findviewbyid (R.id.listview);
Mainlistviewadapter adapter = new Mainlistviewadapter (dishlist);
Mlistview.setadapter (adapter); }//ListView Adapter Private class Mainlistviewadapter extends Baseadapter {private arraylist<dish> Dish
List;
Public Mainlistviewadapter (arraylist<dish> list) {this.dishlist = list;
@Override public int GetCount () {return dishlist.size ();
@Override public Object getitem (int position) {return dishlist.get (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 = Layoutinflater.from (mainactivity.this). Inflate ( R.layout.main_listview_item, NULL);
item = new Listviewitemholder ();
Item.img_iv = (ImageView) convertview. Findviewbyid (R.id.imageview1);
Item.name_textview = (TextView) convertview. Findviewbyid (R.id.textview1);
Item.price_textview = (TextView) convertview. Findviewbyid (R.ID.TEXTVIEW2);
Convertview.settag (item);
else {item = (Listviewitemholder) convertview.gettag ();
} Dish Dish = Dishlist.get (position);
This is where the network image is loaded asynchronously Picasso.with (mainactivity.this). Load (Dish.getimgurl ()). into (ITEM.IMG_IV);
Item.name_textview.setText (Dish.getname ());
Item.price_textview.setText (Dish.getprice () + "Yuan");
return convertview;
}//ListView Item Component Class Private class Listviewitemholder {ImageView Img_iv;
TextView Name_textview;
TextView Price_textview; }
}
V. Summary
The above is the entire content of this article, see here, is not already in love with Picasso, go and it happy play play it, hope this article for everyone's study and work can help.