浅谈Glide<入门>

来源:互联网 发布:8051单片机复位电路 编辑:程序博客网 时间:2024/05/29 17:29

浅谈Glide<入门>

在andorid中图片框架的使用对项目的图片优化和体验起到至关重要的作用,好久没有用glide,重新整理
首先介绍glide的优点<只做介绍,在之后的源码分析中着重分析>: 专注于处理平滑滑动的图片类库
默认使用HttpUrlConnection下载图片
支持设置渐渐显示的动画
支持设置加载中的图片
ok~直接上代码:
首先第一步,在as中添加glide的依赖:

compile 'com.github.bumptech.glide:glide:3.7.0'

然后代码实现glide的代码实现图片的加载:

class 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) {            //使用Glide加载图片//          Glide.with(holder.tvImage.getContext())//                 .load(Constants.IMAGES[position])//                 .centerCrop()//设置从中间剪切//                 .placeholder(R.mipmap.ic_launcher)//设置默认图片//                 .error(R.mipmap.error)//设置error失败的图片//                 .crossFade(1000)//                 .into(holder.tvImage);

下面的是自定义一个utils中存放imageView的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"    };}

在上面的代码中介介绍了glide的一系列用法,包括load图片的url,设置图片从中间剪裁,设置加载错误的图片等,有没有感觉很简单,但是glide之所以可以链式简介的实现功能,必然是glide的源码实现不简单,所以需要关注并且研究其源码~ 在之后博客中会有更 谢谢关注

原创粉丝点击