1,介绍
-
GitHub地址: https://github.com/square/picasso
-
Square开源的比较早的图片加载类库
-
自动处理adapter中的ImageView的回收时取消下载图片
-
支持加载多种来源的图片,比如网络,sd卡,res资源
-
支持设置占位图片
-
支持对图片的自定义处理
-
2,简单使用
【1】添加依赖
compile 'com.squareup.picasso:picasso:2.5.2'
【2】使用
//使用Picasso加载图片
Picasso.with(holder.tvImage.getContext())
.load(Constants.IMAGES[position])
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.error)
.centerCrop()
.noFade()//设置不需要渐渐显示的动画效果
.resize(400,400)//指定压缩参考的宽高比 ,必须传入值否则压缩失败
.into(holder.tvImage);
3,案例
【1】实现效果
【2】实习逻辑
-
添加依赖
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:recyclerview-v7:25.1.0'
-
LoadImageActivity创建布局里面放置RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/recyclerView"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
RecyclerView获取控件设置竖直摆放效果
recyclerView.setLayoutManager(new LinearLayoutManager(this));
-
设置adapter
c
lass MyAdapter extends RecyclerView.Adapter<MyHolder> {
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyHolder(View.inflate(parent.getContext(), R.layout.adapter_image, null));
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
//使用Picasso加载图片
Picasso.with(holder.tvImage.getContext())
.load(Constants.IMAGES[position])
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.error)
.centerCrop()
.noFade()//设置不需要渐渐显示的动画效果
.resize(400,400)//指定压缩参考的宽高比 ,必须传入值否则压缩失败
.into(holder.tvImage);
}
@Override
public int getItemCount() {
return Constants.IMAGES.length;
}
}
class MyHolder extends RecyclerView.ViewHolder {
@Bind(R.id.tv_image)
ImageViewtvImage;
MyHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
-
adapter_image 布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:minWidth="2000dp" //不设置值,布局无法居中
android:padding="10dp"
android:layout_height="match_parent">
<ImageView
android:layout_gravity="center"
android:id="@+id/tv_image"
android:layout_width="200dp"
android:layout_height="200dp" />
</FrameLayout>
访问的图片Url路径
public interface Constants {
String[] IMAGES = new String[] {
// Heavy images
"http://a.hiphotos.baidu.com/image/w%3D310/sign=c21ca7270b24ab18e016e73605fbe69a/728da9773912b31b202b8f288418367adab4e168.jpg",
"http://e.hiphotos.baidu.com/image/pic/item/5243fbf2b2119313c4d3242166380cd790238d8b.jpg",
"http://f.hiphotos.baidu.com/image/pic/item/8b13632762d0f7035486c57d0bfa513d2697c55f.jpg",
"http://g.hiphotos.baidu.com/image/pic/item/c995d143ad4bd113050ef62059afa40f4afb05e4.jpg",
"http://b.hiphotos.baidu.com/image/pic/item/38dbb6fd5266d0169843e151952bd40735fa3574.jpg",
"http://tech.ccidnet.com/col/attachment/2014/1/2684431.jpg",
"http://ww1.sinaimg.cn/large/bc9f5ba3jw1dzaj6clv1nj.jpg",
"http://img1.soufun.com/album/2012_02/21/1329815025308_000.jpg",
"http://i3.tietuku.com/2ad60923cfbf20a9s.jpg",
"http://img5.imgtn.bdimg.com/it/u=4292479122,1538056941&fm=21&gp=0.jpg",
"http://img0.imgtn.bdimg.com/it/u=4220365430,137606180&fm=21&gp=0.jpg",
"http://f6.topit.me/6/a7/7f/11165554671437fa76o.jpg",
"http://f7.topit.me/7/46/f1/11908078649c6f1467o.jpg",
"http://img4.duitang.com/uploads/blog/201401/12/20140112183529_cVnxn.jpeg",
"http://img5.duitang.com/uploads/item/201402/28/20140228010352_t4vkJ.thumb.700_0.jpeg",
"http://cdnq.duitang.com/uploads/item/201401/24/20140124143540_te3Wx.jpeg",
"http://cdn.duitang.com/uploads/blog/201401/12/20140112173332_e5dPj.jpeg",
"http://img1.imgtn.bdimg.com/it/u=933619302,2099219215&fm=21&gp=0.jpg",
"http://img6.faloo.com/Picture/0x0/1/1462/1462375.jpg",
"http://i1.topit.me/1/6f/e2/1142027295f98e26f1o.jpg",
"http://p2.gexing.com/G1/M00/FA/49/rBACE1KjHSDQsqrOAACYniqRbZo130.jpg",
"http://img4.duitang.com/uploads/item/201208/13/20120813120105_R3SAz.thumb.600_0.jpeg",
"http://p4.gexing.com/qqpifu/20121006/0918/506f870311354.jpg",
"http://p2.gexing.com/qqpifu/20120906/0136/50478d8874c30.jpg",
"http://dota2.yzz.cn/pc/img/dota/11.jpg",
"http://img.wy.073img.com/uploads/allimg/130708/27_130708101847_1.jpg",
"http://images.17173.com/2013/news/2013/06/13/mj0613co12s.jpg",
"http://img4.imgtn.bdimg.com/it/u=427763304,3519674062&fm=21&gp=0.jpg",
"http://img5.imgtn.bdimg.com/it/u=2997221812,1149745093&fm=21&gp=0.jpg",
"http://img1.imgtn.bdimg.com/it/u=2550640260,3138080897&fm=21&gp=0.jpg",
"http://images.17173.com/2013/news/2013/01/14/hy0114ai07s.jpg",
"http://pic5.duowan.com/aion/0908/113237346239/113237846350.jpg",
"http://img3.178.com/aion/201102/92705832634/92706128419.jpg",
"http://pic5.duowan.com/aion/1008/145811079570/145811886489.jpg",
"http://img5.imgtn.bdimg.com/it/u=609909125,3962392516&fm=21&gp=0.jpg",
"http://e.hiphotos.baidu.com/image/h%3D200/sign=56f6f02b2b738bd4db21b531918a876c/6a600c338744ebf8960eebc9dcf9d72a6159a7cd.jpg",
"http://g.hiphotos.baidu.com/image/pic/item/50da81cb39dbb6fd9caad8ba0c24ab18962b37bc.jpg",
"http://img3.imgtn.bdimg.com/it/u=799759480,1513466093&fm=21&gp=0.jpg",
"http://www.cgrealm.org/u/upload/2007/12/27/27644.jpg",
"http://img1.cache.netease.com/catchpic/2/24/2414155BD7E04FFA4628E39DF11B21F1.jpg",
"http://img.taopic.com/uploads/allimg/121212/267865-12121222403949.jpg",
"http://s1.it.itc.cn/z/forum_attachment/day_110522/1105221941f496ba48e003c9bc.jpg",
"http://img1.cache.netease.com/catchpic/2/24/2414155BD7E04FFA4628E39DF11B21F1.jpg"
};
}