1. Introduction to Picasso
Picasso is a powerful image library for downloading and caching images produced by Square. Official Website: http://square.github.io/picasso/
You only need a line of code to download the image and set it to ImageView.
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
2. Main Features
2.1 Adapter downloanalyticdb
When using ListView and GridView, the system automatically checks the reuse of the Adapter (re-use), cancels the download, and uses the cache.
@Override public void getView(int position, View convertView, ViewGroup parent) { SquaredImageView view = (SquaredImageView) convertView; if (view == null) { view = new SquaredImageView(context); } String url = getItem(position); Picasso.with(context).load(url).into(view);}
2.2 image processing and transformation
Transform the image to better adapt to layout controls and reduce memory overhead.
Picasso.with(context) .load(url) .resize(200, 200) .centerCrop() .into(imageView)
Of course, we can also write our own Transformation class, but we must implement the Transformation interface, such:
/*** Custom interface to reduce the image to half the original size */public class CropSquareTransformation implements Transformation {@ Overridepublic Bitmap transform (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 ;}@ Overridepublic String key () {return "square ()";}}
Then you can set the transform method:
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").transform(new CropSquareTransformation()).into(iv_test2);
As follows:
2.3. Allows you to set images before loading and images after loading failure.
For example: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD48cD48cHJlIGNsYXNzPQ = "brush: java;"> Picasso. with (this ). load ("http:// I .imgur.com/DvpvklR.png "). placeholder (R. drawable. abc ). error (R. drawable. ic_launcher ). transform (new CropSquareTransformation ()). into (iv_test1 );
When imageviewis created, abc.png is displayed. If the upload succeeds, dvpvklr.png is displayed. If the upload fails, ic_launcher.png is displayed.
2.4 supports loading local images and image files in sdcard.
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);Picasso.with(context).load(new File(...)).into(imageView2);
Picasso: http://square.github.io/picasso/
Not for commercial purposes without permission