我的博客:http://mrfufufu.github.io/
一、前言
最近在整理项目中的一些代码,以备即将开展的新项目中使用,刚刚整理到一个图片加载的 lib,用起来非常的简单,和 picasso 或者谷歌的 Volley 等都一样,只需要一行代码就能完成图片加载的逻辑。
项目地址:ImageViewEx
项目效果:
二、使用
先给出使用方法:首先在布局文件中加入如下代码:
<mrfu.imageviewex.lib.ImageViewEx
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@+id/toolbar"
android:scaleType="centerCrop" />
如果是要使用圆形或者圆角的图片,只需要将 ImageViewEx
改成 RoundImageView
就可以了。
在 Java 代码中,这样写就可以了
ImageViewEx imageviewex = (ImageViewEx)findViewById(R.id.imageview);
RoundImageView roundimageview1 = (RoundImageView)findViewById(R.id.roundimageview1);
RoundImageView roundimageview2 = (RoundImageView)findViewById(R.id.roundimageview2);
imageviewex.loadImage("http://f.hiphotos.baidu.com/image/pic/item/ae51f3deb48f8c5471a15c2e38292df5e0fe7f45.jpg");
roundimageview1.loadImage("http://f.hiphotos.baidu.com/image/pic/item/ae51f3deb48f8c5471a15c2e38292df5e0fe7f45.jpg");
roundimageview2.setConer(10, 10);//设置圆角图片
roundimageview2.loadImage("http://f.hiphotos.baidu.com/image/pic/item/ae51f3deb48f8c5471a15c2e38292df5e0fe7f45.jpg");
三、 原理
先给出类关系图
使用方式非常简单,现在大概来分析一下这个框架的原理:
框架的加载方式主要还是通过 http 请求的 get 方式拿到图片,然后将其保存在 SD 卡中,将 SD 卡中的图片转化为 Bitmap 对象,通过 LruMemoryCache
缓存方式存到内存中。并将其加载到对应 View 上去。
主要类就是 ImageViewEx
和 ImageLoader