Android: Powerful Picasso library for downloading and downloading images

Source: Internet
Author: User

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

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.