【Android之Picasso图形缓存库】

 A powerful image downloading and caching library for Android

 

 

Images add much-needed context and visual flair to Android applications. Picasso allows for hassle-free image loading in your application—often in one line of code!

Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);

Many common pitfalls of image loading on Android are handled automatically by Picasso:

  • Handling ImageView recycling and download cancelation in an adapter.
  • Complex image transformations with minimal memory use.
  • Automatic memory and disk caching.

许多在Android上图片加载常见的的陷阱都被Picasso自动的处理了:

1)在adaper中处理ImageView循环和取消下载

2)对复杂图像进行转换,使其占用最小的内存

3)自动的内存和磁盘缓存

Features

ADAPTER DOWNLOADS

Adapter re-use is automatically detected and the previous download canceled.

IMAGE TRANSFORMATIONS

Transform images to better fit into layouts and to reduce memory size.

You can also specify custom transformations for more advanced effects.

PLACE HOLDERS

Picasso supports both download and error placeholders as optional features.

A request will be retried three times before the error placeholder is shown.

RESOURCE LOADING

Resources, assets, files, content providers are all supported as image sources.

Picasso.get().load(R.drawable.landing_screen).into(imageView1);

Picasso.get().load("file:///android_asset/DvpvklR.png").into(imageView2);

Picasso.get().load(new File(...)).into(imageView3);

DEBUG INDICATORS

For development you can enable the display of a colored ribbon which indicates the image source. Call setIndicatorsEnabled(true) on the Picasso instance.

特性

在Adapter中下载

自动检测Adapter重用并取消之前的下载

图片转换

转换图片以更好地适配布局并减少内存使用

你也可以指定定制的转换方式来实现更高级的效果

占位图

Picasso同时支持了下载和出错的占位图供用户选择

资源加载

支持Resources, assets, files, content providers作为图片源

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);

Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);

Picasso.with(context).load(new File(...)).into(imageView3);

调试指示

开发时可以打开彩带显示来指示图片源,在Picasso实例调用setIndicatorsEnabled(true)即可

Picasso功能

 1.加载速度快 
 2.资源消耗低 
 3.保证加载图片时不错位 
  4.加载图片类型丰富,支持网络图片以及各种本地图片如Asserts目录下,内容提供者提供的图片资源路径

Picasso策略

  1.加载速度快 
  标准的二级缓存(内存缓存+磁盘缓存)+Net 
  a.标配策略,MemoryCache+DiskCache+Net。提高加载速度,同时保证流量。 
  b.Net部分,兼顾单请求加载速度与多请求并发能力,从而提高整体加载速度。 
c.MemoryCache部分,通过Lru策略提高缓存效率。 
  2.资源消耗低 
  a.渲染适当尺寸图片来减少内存。 
  b.通过线程池来限制并发的图片加载线程,降低资源消耗。 
  c.请求相同图片的线程要合并,减少线程数。 
  3.加载图片类型丰富 
  Picasso内置针对不同的图片资源类型的各种handler 例如:网络下载图片:NetworkRequestHandler,Asserts目录下的图片资源:AssetRequestHandler 
 4.保证加载图片时不错位 
  Picasso维护了Map,Key为ImageView,Value为Action,每个ImageView均只对应一个Action。若获取的图片Action与ImageView不符合,则丢弃,等待正确的Action执行完。 

猜你喜欢

转载自gaojingsong.iteye.com/blog/2418646