GitHub开源:https://github.com/bumptech/glide
超棒的工具,竟然还可以加载.gif !!Glide完全基于Picasso,沿袭了其简洁风格,并在其基础上做了大量优化与改进。
1、Glide默认的Bitmap格式是RGB_565,而Picasso默认ARGB_8888,Glide内存开销要小一半。
2、磁盘存储方面,Picasso只缓存原始图片,而Glide可以根据ImageView大小来缓存相应图片大小,这个改进使Glide速度更快。
3、Glide支持加载.gif动图,Picasso不支持。
总体来说,Glide是在Picasso基础上进行的二次开发,各方面做了不少改进,但也导致Glide的包比较大(<500k),Picasso是100k左右。
一篇两者的比较:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0327/2650.html
github介绍使用方法:
// For a simple view:
@Override public void onCreate(Bundle savedInstanceState) {...ImageView imageView = (ImageView) findViewById(R.id.my_image_view);Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
}// For a simple image list:
@Override public View getView(int position, View recycled, ViewGroup container) {final ImageView myImageView;if (recycled == null) {myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false);} else {myImageView = (ImageView) recycled;}String url = myUrls.get(position);Glide.with(myFragment).load(url).centerCrop().placeholder(R.drawable.loading_spinner).crossFade().into(myImageView);return myImageView;
}
实例:在ListView中使用Glide加载.gif和.jpg:
在适配器Adapter的getView()中(比Universal-Image-loader简洁太多啦!):
@Overridepublic View getView(int position,View convertView,ViewGroup parent){ViewHolder viewHolder;View view;if(convertView == null){viewHolder = new ViewHolder();view = LayoutInflater.from(getContext()).inflate(resource, null);viewHolder.imageView = (ImageView)view.findViewById(R.id.image_item); view.setTag(viewHolder);}else{view = convertView;viewHolder = (ViewHolder)view.getTag();}String imageUrl = Urls.imageUrls[position];Glide.with(getContext()).load(imageUrl).centerCrop().crossFade().into(viewHolder.imageView);return view;}//.override(int width,int height).placeholder(R.drawable.ic_launcher).error(R.drawable.ic_launcher)class ViewHolder{ImageView imageView;}
Glide.with()
** 总结:可以传入的参数有*@see #with( Android .app.Activity)
* @see #with(android.app.Fragment)
* @see #with(android.support.v4.app.Fragment)
* @see #with(android.support.v4.app.FragmentActivity)
centerCrop是一种裁剪技术,缩放图像,让它填充到ImageView.
crossFade(),淡入淡出效果
into(),imageView的id.
Urls.java
package sunny.example.helloglidelistview;public class Urls {final static String[] imageUrls = new String[]{"http://img.ui.cn/data/file/8/0/2/666208.gif","http://img.ui.cn/data/file/8/3/8/667838.jpg","http://img.ui.cn/data/file/9/5/9/654959.jpg","http://img.ui.cn/data/file/7/0/3/668307.jpg","http://img.ui.cn/data/file/6/0/1/663106.jpg","http://img.ui.cn/data/file/6/5/8/664856.jpg","http://img.ui.cn/data/file/0/2/2/666220.jpg","http://img.ui.cn/data/file/2/5/1/667152.gif","http://img.ui.cn/data/file/3/5/1/667153.jpg","http://img.ui.cn/data/file/8/3/8/664838.jpg"};
}
Thanks to http://www.ui.cn/
Item.java
public class Item {private String imageUrl;public Item(String imageUrl){this.imageUrl = imageUrl;}}
MainActivity.java
package sunny.example.helloglidelistview;import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;public class MainActivity extends ActionBarActivity {private ListView listView;private ArrayList<Item> arrayList;private Item item;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ListView)findViewById(R.id.listView);arrayList = new ArrayList<Item>();for(int i = 0;i < Urls.imageUrls.length;i++){item = new Item(Urls.imageUrls[i]);arrayList.add(item);}ItemAdapter adapter = new ItemAdapter(this,R.layout.image_item,arrayList);listView.setAdapter(adapter);}}
完整代码在:https://github.com/HiSunny/HelloGlideListView.git
Thanks to stromzhang