Android: Powerful image download and cache library Picasso

Source: Internet
Author: User

1.Picasso Brief Introduction

Picasso is a powerful image download and cache Gallery for the square company. The official website is:http://square.github.io/picasso/

Simply need a code to download and set the image on the ImageView.

Picasso.with (context). Load ("Http://i.imgur.com/DvpvklR.png"). into (ImageView);


2. Main Features

2.1Adapter Downloads

When using Listview,gridview, you actively detect adapter reuse (re-use), cancel the download, and use the cache.

@Override public void GetView (int position, view Convertview, ViewGroup parent) {  Squaredimageview View = (squaredimag Eview) 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 fit layout controls, and reduce memory overhead.

Picasso.with (context).  load (URL).  resize ($).  Centercrop (). into  (ImageView)

Of course, we can also write our own transformation class, but we must implement the transformation interface, such as:

/** * Define the interface itself, realize the image is reduced to half the original */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);

For example, the following:



2.3. Supports setting up images before loading, and loading failed images.

Such as:

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);

ImageView display Abc.png when created, assuming the load succeeds, the dvpvklr.png is displayed, assuming the load fails, the ic_launcher.png is displayed.

2.4 Support for 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 consent

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.