简介:
Picasso是Square公司开源的一个Android图形缓存库。可以实现图片下载和缓存功能。
特点:
1.加载载网络或本地图片并自动缓存处理;
2.链式调用;
3.图形转换操作,如变换大小,旋转等,提供了接口来让用户可以自定义转换操作;
4.在Adapter中回收和取消当前的下载功能;
与Universal-ImageLoader库对比:
1.都有高效的网络图片下载和缓存性能;
2.Universal-ImageLoader功能多,灵活使用配置;3.Picasso使用复杂的图片压缩转换来尽可能的减少内存消耗;4.在Adapter中需要取消已经不在视野范围的ImageView图片资源的加载,否则会导致图片错位,Picasso已经解决了这个问题;
用法:
1.图片转换:转换图片以适应布局大小并减少内存占用
Picasso.with(context).load(url).resize(50, 50) .centerCrop() .into(imageView);2.Adapter 中的下载:Adapter的重用会被自动检测到,Picasso会取消上次的加载;3.空白或者错误占位图片设置方法及本地资源文件的加载方法;4.Picasso采用链式调用加载和处理图片方式;
5.除了加载网络图片,picasso还支持加载Resources, assets, files, content providers中的本地资源文件;
下面实现一个ListView显示网络图片:
这里找了四张网络菜谱图片,在代码中新添加一个菜类:
- //表示菜类(经过烹调的蔬菜、蛋品、肉类等)
- public class Dish {
- private String imgUrl; // 图片地址
- private String name; // 菜名
- private String price; // 菜价
- 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;
- }
- }
主界面类中,调用load方法加载网络图片,调用into方法将图片设置给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://img1.3lian.com/img2011/w1/106/85/";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ArrayList<Dish> dishList = new ArrayList<Dish>();
- dishList.add(new Dish(BASE_URL + "42.jpg", "水煮鱼片", "38.00"));
- dishList.add(new Dish(BASE_URL + "34.jpg", "小炒肉", "18.00"));
- dishList.add(new Dish(BASE_URL + "37.jpg", "清炒时蔬", "15.00"));
- dishList.add(new Dish(BASE_URL + "11.jpg", "金牌烤鸭", "36.00"));
- dishList.add(new Dish(BASE_URL + "12.jpg", "粉丝肉煲", "20.00"));
- ListView mListView = (ListView) this.findViewById(R.id.listview);
- MainListViewAdapter adapter = new MainListViewAdapter(dishList);
- mListView.setAdapter(adapter);
- }
- // ListView适配器
- private class MainListViewAdapter extends BaseAdapter {
- private ArrayList<Dish> dishList;
- public MainListViewAdapter(ArrayList<Dish> list) {
- this.dishList = list;
- }
- @Override
- public int getCount() {
- return dishList.size();
- }
- @Override
- public Object getItem(int position) {
- return dishList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ListViewItemHolder item = null;
- if (convertView == 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);
- //这里就是异步加载网络图片的地方
- Picasso.with(MainActivity.this).load(dish.getImgUrl())
- .into(item.img_iv);
- item.name_textview.setText(dish.getName());
- item.price_textview.setText(dish.getPrice() + "元");
- return convertView;
- }
- }
- // ListView的Item组件类
- private class ListViewItemHolder {
- ImageView img_iv;
- TextView name_textview;
- TextView price_textview;
- }
- }
下面是整个工程的下载链接,有兴趣的可以下载导入到自己的Eclipse中去运行:
Android之Picasso - bear_huangzhen的专栏 - 博客频道 - CSDN.NET
http://blog.csdn.net/bear_huangzhen/article/details/45868755