Android's Picasso

Source: Internet
Author: User

Brief introduction:

Picasso is an open source Android graphics Cache library for square company. You can implement image download and caching functions.


Characteristics:

1. Load the network or local image and automatically cache the processing;

2. Chaining calls;

3. Graphics conversion operations, such as transformation size, rotation, etc., provide an interface to allow users to customize the conversion operation;

4. Recycle and cancel the current download function in adapter;


Compare to Universal-imageloader Library:

1. Have efficient network image download and cache performance;
2.universal-imageloader function, flexible use configuration;
3.Picasso use complex image compression conversion to reduce memory consumption as much as possible;
4. In the adapter need to cancel already not in the field of view of the ImageView picture resource loading, otherwise it will cause the picture dislocation, Picasso has solved the problem;


Usage:

1. Picture conversion: Convert pictures to fit layout size and reduce memory footprint
Picasso.with (context). Load (URL). Resize (). Centercrop (). into (ImageView);
Download in 2.Adapter: Adapter reuse will be automatically detected, Picasso will cancel the last load;
3. Blank or error placeholder picture setting method and local resource file loading method;

4.Picasso use chain call to load and process the image mode;

5. In addition to loading the network image, Picasso also supports loading resources, assets, files, content providers local resource files;


The following implements a ListView display network Picture:

Here are four web recipe images, add a new dish in the code:

Denotes a vegetable (vegetable, egg, meat, etc.) public class Dish {private string Imgurl;//Picture address private string name;//dish name private string price;// Menu 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, call the Load method to load the network picture and call the Into method to set the picture to a component such as ImageView:

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://i mg1.3lian.com/img2011/w1/106/85/"; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); arraylist<dish> dishlist = new arraylist<dish> ();d Ishlist.add (New Dish (Base_url + "42.jpg", "Poached Fillet", "38.00" );d Ishlist.add (New Dish (Base_url + "34.jpg", "Fried meat", "18.00"));d Ishlist.add (New Dish (Base_url + "37.jpg", "stir fry vegetables", "15.00 ");d Ishlist.add (New Dish (Base_url +" 11.jpg "," Golden Roast Duck "," 36.00 "));d Ishlist.add (New Dish (Base_url +" 12.jpg "," Vermicelli Stew "," 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> Dishlist;public Mainlistviewadapter (arraylist<dish> list) {this.dishlist = list;} @Overridepublic int GetCount () {return dishlist.size ();} @Overridepublic Object getItem (int position) {return dishlist.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Listviewitemholder item = null;if (convert View = = 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;}} The item component class of the ListView is private class Listviewitemholder {ImageView Img_iv; TextView Name_textview; TextView Price_textview;}}

here is a download link for the entire project, interested in downloading and importing into your own eclipse to run:

Android Picasso


Android's Picasso

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.