什么是图片的三级缓存
- 1、内存缓存 优先加载,速度最快
- 2、本地缓存 次优先加载 速度稍快
- 3、网络缓存 最后加载 速度由网络速度决定(浪费流量)
图片缓存原理
内存-文件-网络 三层cache机制,其中内存缓存包括强引用缓存和软引用缓存,其实网络不算Cache。当根据url向网络加载图片的时候,先从内存中找,如果内存中没有,再从缓存文件中查找,如果缓存文件中也没有,再从网络上通过http请求加载图片。在键值对(key-value)中,这个图片缓存的key是图片url的hash值,value就是Bitmap。所以,按照这个逻辑,只要一个url被下载过,其实图片就被缓存起来了。
LruCache
内存缓存技术对那些大量占用应用程序宝贵内存的图片提供了快速访问的方法。其中最核心的类是LruCache(此类在android-support-v4的包中提供) 。这个类非常适合用来缓存图片,它的主要算法原理是把最近使用的对象用强引用存储在LinkedHashMap 中,并且把最近最少使用的对象在缓存值达到预设定值之前从内存中移除。
DiskLruCache
LruCache只是管理了内存中图片的存储与释放,如果图片从内存中被移除的话,那么又需要从网络上重新加载一次图片,这显然非常耗时。对此,Google又提供了一套硬盘缓存的解决方案:DiskLruCache(非Google官方编写,但获得官方认证)。只可惜,Android Doc中并没有对DiskLruCache的用法给出详细的说明,而网上关于DiskLruCache的资料也少之又少。
图片的三级缓存图形解释
好了,既然知道了什么三级缓存的字面意思了,那么我们就来处理吧。
图片缓存—内存缓存的原理
MemoryCacheUtils.java:
<code class="hljs java has-numbering" style="margin: 8px 0px; display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package</span> com.example.bitmaputils.bitmap; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.graphics.Bitmap; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.util.Log; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.util.LruCache; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 内存缓存 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">MemoryCacheUtils</span> {</span><span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * LinkedHashMap<>(10,0.75f,true); * <p/> * 10是最大致 0.75f是加载因子 true是访问排序 false插入排序 * * */</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//private LinkedHashMap<String,Bitmap> mMemoryCache = new LinkedHashMap<>(5,0.75f,true);</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> LruCache<String, Bitmap> mLruCache; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">MemoryCacheUtils</span>() { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">long</span> maxMemory = Runtime.getRuntime().maxMemory();<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//最大内存 默认是16兆 运行时候的</span> mLruCache = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> LruCache<String, Bitmap>((<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>) (maxMemory / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>)) { <span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> <span class="hljs-title" style="box-sizing: border-box;">sizeOf</span>(String key, Bitmap value) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//int byteCount = value.getByteCount();</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//得到图片字节数</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// @return number of bytes between rows of the native bitmap pixels.</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> byteCount = value.getRowBytes() * value.getWidth(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> byteCount; } }; } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 从内存中读取 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> url */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> Bitmap <span class="hljs-title" style="box-sizing: border-box;">getFromMemroy</span>(String url) { Log.d(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"MyBitmapUtils"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"从内存中加载图片"</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> mLruCache.get(url); } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 写入到内存中 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> url *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> bitmap */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">setToMemory</span>(String url, Bitmap bitmap) { mLruCache.put(url, bitmap); } } </code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">1</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">2</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">3</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">4</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">5</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">6</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">7</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">8</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">9</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">10</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">11</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">12</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">13</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">14</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">15</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">16</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">17</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">18</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">19</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">20</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">21</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">22</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">23</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">24</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">25</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">26</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">27</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">28</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">29</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">30</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">31</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">32</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">33</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">34</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">35</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">36</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">37</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">38</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">39</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">40</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">41</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">42</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">43</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">44</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">45</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">46</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">47</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">48</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">49</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">50</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">51</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">52</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">53</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">54</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">55</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">56</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">57</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">58</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">59</li></ul>
很简单吧,在这里我们使用了LruCache(),对图片进行了内存缓存,这里我们只是稍微进行了处理,在这里我们只是介绍原理,当然了哈,性能,OOM溢出问题我们在这里不作处理。
图片缓存—本地缓存
<code class="hljs java has-numbering" style="margin: 8px 0px; display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package</span> com.example.bitmaputils.bitmap; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.graphics.Bitmap; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.graphics.BitmapFactory; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.os.Environment; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.util.Log; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> com.example.bitmaputils.MD5Encoder; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.io.File; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.io.FileInputStream; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.io.FileNotFoundException; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.io.FileOutputStream; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 本地缓存 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">SDcardCacheUtils</span> {</span><span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 我们读取内存的绝对路径 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">final</span> String CACHE_PATH = Environment .getExternalStorageDirectory().getAbsolutePath() + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/aixuexi"</span>; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 从本地读取 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> url */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> Bitmap <span class="hljs-title" style="box-sizing: border-box;">getFromSd</span>(String url){ String fileName = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//得到图片的url的md5的文件名</span> fileName = MD5Encoder.encode(url); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (Exception e) { e.printStackTrace(); } File file = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> File(CACHE_PATH,fileName); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果存在,就通过bitmap工厂,返回的bitmap,然后返回bitmap</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (file.exists()){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { Bitmap bitmap = BitmapFactory.decodeStream(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> FileInputStream(file)); Log.d(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"MyBitmapUtils"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"从本地读取图片啊"</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> bitmap; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (FileNotFoundException e) { e.printStackTrace(); } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 向本地缓存 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> url 图片地址 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> bitmap 图片 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">savaSd</span>(String url,Bitmap bitmap){ String fileName = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//我们对图片的地址进行MD5加密,作为文件名</span> fileName = MD5Encoder.encode(url); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (Exception e) { e.printStackTrace(); } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 以CACHE_PATH为文件夹 fileName为文件名 */</span> File file = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> File(CACHE_PATH,fileName); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//我们首先得到他的符文剑</span> File parentFile = file.getParentFile(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//查看是否存在,如果不存在就创建</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!parentFile.exists()){ parentFile.mkdirs(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建文件夹</span> } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//将图片保存到本地</span><span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> format The format of the compressed image 图片的保存格式 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> quality Hint to the compressor, 0-100. 0 meaning compress for * small size, 100 meaning compress for max quality. Some * formats, like PNG which is lossless, will ignore the * quality setting * 图片的保存的质量 100最好 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> stream The outputstream to write the compressed data. */</span> bitmap.compress(Bitmap.CompressFormat.JPEG,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> FileOutputStream(file)); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (FileNotFoundException e) { e.printStackTrace(); } } } </code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">1</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">2</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">3</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">4</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">5</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">6</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">7</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">8</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">9</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">10</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">11</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">12</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">13</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">14</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">15</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">16</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">17</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">18</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">19</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">20</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">21</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">22</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">23</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">24</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">25</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">26</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">27</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">28</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">29</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">30</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">31</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">32</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">33</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">34</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">35</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">36</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">37</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">38</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">39</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">40</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">41</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">42</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">43</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">44</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">45</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">46</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">47</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">48</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">49</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">50</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">51</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">52</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">53</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">54</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">55</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">56</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">57</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">58</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">59</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">60</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">61</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">62</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">63</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">64</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">65</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">66</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">67</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">68</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">69</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">70</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">71</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">72</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">73</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">74</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">75</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">76</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">77</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">78</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">79</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">80</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">81</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">82</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">83</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">84</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">85</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">86</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">87</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">88</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">89</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">90</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">91</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">92</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">93</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">94</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">95</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">96</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">97</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">98</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">99</li></ul>
图片缓存—网络缓存
NetCacheUtils .java:
<code class="hljs java has-numbering" style="margin: 8px 0px; display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package</span> com.example.bitmaputils.bitmap; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.graphics.Bitmap; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.graphics.BitmapFactory; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.os.AsyncTask; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.util.Log; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.widget.ImageView; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.io.IOException; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.io.InputStream; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.net.HttpURLConnection; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> java.net.URL; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 网络缓存工具类 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">NetCacheUtils</span> {</span><span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 图片 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> ImageView mImageView; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 图片地址 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> String mUrl; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 本地缓存 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> SDcardCacheUtils mDcardCacheUtils; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 内存缓存 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> MemoryCacheUtils mMemoryCacheUtils; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">NetCacheUtils</span>(SDcardCacheUtils dcardCacheUtils, MemoryCacheUtils memoryCacheUtils) { mDcardCacheUtils = dcardCacheUtils; mMemoryCacheUtils = memoryCacheUtils; } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 从网络中下载图片 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> image *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> url */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">getDataFromNet</span>(ImageView image, String url) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> MyAsyncTask().execute(image, url); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//启动Asynctask,传入的参数到对应doInBackground()</span> } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 异步下载 * <p/> * 第一个泛型 : 参数类型 对应doInBackground() * 第二个泛型 : 更新进度 对应onProgressUpdate() * 第三个泛型 : 返回结果result 对应onPostExecute */</span> class MyAsyncTask extends AsyncTask<Object, Void, Bitmap> { <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 后台下载 子线程 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> params *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span> */</span><span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> Bitmap <span class="hljs-title" style="box-sizing: border-box;">doInBackground</span>(Object... params) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//拿到传入的image</span> mImageView = (ImageView) params[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//得到图片的地址</span> mUrl = (String) params[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//将imageview和url绑定,防止错乱</span> mImageView.setTag(mUrl); Bitmap bitmap = downLoadBitmap(mUrl); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> bitmap; } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 进度更新 UI线程 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> values */</span><span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onProgressUpdate</span>(Void... values) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.onProgressUpdate(values); } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 回调结果,耗时方法结束后,主线程 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> bitmap */</span><span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@Override</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onPostExecute</span>(Bitmap bitmap) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (bitmap != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//得到图片的tag值</span> String url = (String) mImageView.getTag(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//确保图片设置给了正确的image</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (url.equals(mUrl)) { mImageView.setImageBitmap(bitmap); <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 当从网络上下载好之后保存到sdcard中 */</span> mDcardCacheUtils.savaSd(mUrl, bitmap); <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 写入到内存中 */</span> mMemoryCacheUtils.setToMemory(mUrl, bitmap); Log.d(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"MyBitmapUtils"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"我是从网络缓存中读取的图片啊"</span>); } } } } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 下载图片 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> url 下载图片地址 *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @return</span> */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> Bitmap <span class="hljs-title" style="box-sizing: border-box;">downLoadBitmap</span>(String url) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//连接</span> HttpURLConnection conn = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">try</span> { conn = (HttpURLConnection) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> URL(url) .openConnection(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置读取超时</span> conn.setReadTimeout(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5000</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置请求方法</span> conn.setRequestMethod(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"GET"</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//设置连接超时连接</span> conn.setConnectTimeout(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5000</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//连接</span> conn.connect(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//响应码</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> code = conn.getResponseCode(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (code == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//请求正确的响应码是200</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//得到响应流</span> InputStream inputStream = conn.getInputStream(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//得到bitmap对象</span> Bitmap bitmap = BitmapFactory.decodeStream(inputStream); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> bitmap; } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">catch</span> (IOException e) { # e.printStackTrace(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">finally</span> { conn.disconnect(); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>; } } </code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">1</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">2</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">3</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">4</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">5</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">6</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">7</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">8</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">9</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">10</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">11</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">12</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">13</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">14</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">15</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">16</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">17</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">18</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">19</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">20</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">21</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">22</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">23</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">24</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">25</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">26</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">27</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">28</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">29</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">30</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">31</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">32</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">33</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">34</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">35</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">36</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">37</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">38</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">39</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">40</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">41</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">42</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">43</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">44</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">45</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">46</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">47</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">48</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">49</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">50</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">51</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">52</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">53</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">54</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">55</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">56</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">57</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">58</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">59</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">60</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">61</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">62</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">63</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">64</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">65</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">66</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">67</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">68</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">69</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">70</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">71</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">72</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">73</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">74</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">75</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">76</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">77</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">78</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">79</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">80</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">81</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">82</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">83</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">84</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">85</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">86</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">87</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">88</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">89</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">90</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">91</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">92</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">93</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">94</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">95</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">96</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">97</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">98</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">99</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">100</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">101</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">102</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">103</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">104</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">105</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">106</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">107</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">108</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">109</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">110</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">111</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">112</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">113</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">114</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">115</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">116</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">117</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">118</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">119</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">120</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">121</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">122</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">123</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">124</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">125</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">126</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">127</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">128</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">129</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">130</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">131</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">132</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">133</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">134</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">135</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">136</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">137</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">138</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">139</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">140</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">141</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">142</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">143</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">144</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">145</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">146</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">147</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">148</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">149</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">150</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">151</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">152</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">153</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">154</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">155</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">156</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">157</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">158</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">159</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">160</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">161</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">162</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">163</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">164</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">165</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">166</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">167</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">168</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">169</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">170</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">171</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">172</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">173</li></ul>
好了,至此,一个简单的图片的三级缓存完成了,接下来看看使用吧。
图片加载工具类
MyBitmapUtils .java:
<code class="hljs java has-numbering" style="margin: 8px 0px; display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">package</span> com.example.bitmaputils.bitmap; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * Created by 若兰 on 2016/1/29. * 一个懂得了编程乐趣的小白,希望自己 * 能够在这个道路上走的很远,也希望自己学习到的 * 知识可以帮助更多的人,分享就是学习的一种乐趣 * QQ:1069584784 * csdn:http://blog.csdn.net/wuyinlei */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.graphics.Bitmap; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">import</span> android.widget.ImageView; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 自定义的bitmap工具类 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">MyBitmapUtils</span> {</span><span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 网络缓存 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> NetCacheUtils mNetCacheUtils; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 本地缓存 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> SDcardCacheUtils mSdCacheUtils; <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 内存缓存 */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> MemoryCacheUtils mMemoryCacheUtils; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">MyBitmapUtils</span>() { mSdCacheUtils = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> SDcardCacheUtils(); mMemoryCacheUtils = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> MemoryCacheUtils(); mNetCacheUtils = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> NetCacheUtils(mSdCacheUtils, mMemoryCacheUtils); } <span class="hljs-javadoc" style="color: rgb(136, 0, 0); box-sizing: border-box;">/** * 展示图片的方法 * *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> image *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102); box-sizing: border-box;"> @param</span> url */</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">display</span>(ImageView image, String url) { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//从内存中读取</span> Bitmap fromMemroy = mMemoryCacheUtils.getFromMemroy(url); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果内存中有的h话就直接返回,从内存中读取</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (fromMemroy != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { image.setImageBitmap(fromMemroy); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//从本地SD卡读取</span> Bitmap fromSd = mSdCacheUtils.getFromSd(url); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (fromSd != <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">null</span>) { image.setImageBitmap(fromSd); mMemoryCacheUtils.setToMemory(url, fromSd); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>; } <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//从网络中读取</span> mNetCacheUtils.getDataFromNet(image, url); } } </code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">1</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">2</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">3</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">4</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">5</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">6</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">7</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">8</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">9</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">10</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">11</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">12</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">13</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">14</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">15</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">16</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">17</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">18</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">19</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">20</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">21</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">22</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">23</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">24</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">25</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">26</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">27</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">28</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">29</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">30</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">31</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">32</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">33</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">34</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">35</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">36</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">37</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">38</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">39</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">40</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">41</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">42</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">43</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">44</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">45</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">46</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">47</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">48</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">49</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">50</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">51</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">52</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">53</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">54</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">55</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">56</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">57</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">58</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">59</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">60</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">61</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">62</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">63</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">64</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">65</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">66</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">67</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">68</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">69</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">70</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">71</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">72</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">73</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">74</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">75</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">76</li></ul>
使用这个工具类就很简单了
只需在加载数据的适配器中
<code class="hljs cs has-numbering" style="margin: 8px 0px; display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//声明图片加载工具类</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span> MyBitmapUtils utils; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">PhotoAdapter</span>() { <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//mBitmapUtils = new BitmapUtils(MainActivity.this);</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// mBitmapUtils.configDefaultLoadingImage(R.mipmap.defaut);</span> utils = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> MyBitmapUtils(); } 然后在getView()方法中,使用工具类中的display()方法就可以了。简单吧 utils.display(holder.tvImage,mImageViews[position]);</code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">1</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">2</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">3</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">4</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">5</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">6</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">7</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">8</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">9</li><li style="margin: 0px; box-sizing: border-box; padding: 0px 5px;">10</li></ul>
作者:ApplePear1024 发表于2016/8/14 16:05:25 原文链接
阅读:35 评论:0 查看评论