从零开始编写图片加载库(四)之图片缓存MemoryCache
来源:互联网 发布:xcode使用教程c语言 编辑:程序博客网 时间:2024/06/10 01:15
图片加载通过内存加载是最快的,然而手机的内存并不是取之不尽的,所以在开发过程中还需要注意就是内存的使用问题,本节将不涉及内存使用和优化问题,本节只介绍图片下载后从内存中加载。
第一步:编写MemoryCache类
package cn.sundroid.cache;import java.lang.ref.Reference;import java.lang.ref.WeakReference;import java.util.Collections;import java.util.HashMap;import java.util.Map;import android.graphics.Bitmap;public class WeakMemoryCache { /**保存Bitmap的对象*/ private final Map<String, Reference<Bitmap>> softMap = Collections .synchronizedMap(new HashMap<String, Reference<Bitmap>>()); /** * 根据键获得Bitmap * @param key 查询需要的键 * @return */ public Bitmap get(String key) { if (key == null) { throw new NullPointerException("key == null"); } Bitmap result = null; Reference<Bitmap> reference = softMap.get(key); if (reference != null) { result = reference.get(); } return result; } /** * * 保存Bitmap到内存 * @param key 键 * @param value 值 * @return */ public boolean put(String key, Bitmap value) { if (key == null || value == null) { throw new NullPointerException("key == null || value == null"); } softMap.put(key, new WeakReference<Bitmap>(value)); return false; } /** * 根据key从内存中移除Bitmap对象 * @param key * @return */ public Bitmap remove(String key) { Reference<Bitmap> bmpRef = softMap.remove(key); return bmpRef == null ? null : bmpRef.get(); } /** * 清空操作 */ public void clear() { softMap.clear(); }}
第二步:编写实现,下载图片后在handler里面接收更新UI通知,从cache里面取图片。
package cn.sundroid.loader;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import cn.sundroid.cache.WeakMemoryCache;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.widget.ImageView;public class MainActivity extends Activity { private ImageView image; private final String TAG = "ImageLoader"; private WeakMemoryCache cache = new WeakMemoryCache(); private final String IMAGE_CACHE_KEY = "image_cache_key"; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { Bitmap bitmap = cache.get(IMAGE_CACHE_KEY); Log.e(TAG, "get bitmap from memory cache"); image.setImageBitmap(bitmap); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.image); new Thread(new Runnable() { @Override public void run() { try { //网络连接 HttpURLConnection connection = (HttpURLConnection) new URL( "http://pic5.nipic.com/20091228/2588536_142951087553_2.jpg") .openConnection(); connection.setConnectTimeout(3 * 1000); connection.setReadTimeout(20 * 1000); //读取文件流 InputStream is = connection.getInputStream(); //通过BitmapFactory解码二进制字节流成Bitmap Bitmap bitmap = BitmapFactory.decodeStream(is); Log.e(TAG, "put bitmap into memory cache"); cache.put(IMAGE_CACHE_KEY, bitmap); //发送一个消息给Handler用于更新消息 handler.sendEmptyMessage(0x10); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start(); }}
源代码下载==>
0 0
- 从零开始编写图片加载库(四)之图片缓存MemoryCache
- 从零开始编写图片加载库(五)之图片缓存DiskCache
- 从零开始编写图片加载库(二)之AsyncTask图片下载
- 从零开始编写图片加载库(一)之Handler+Thread图片下载
- 从零开始编写图片加载库(三)之线程池图片下载
- MemoryCache和DiskCache在ListView和GridView中加载图片的总结(-)
- MemoryCache和DiskCache在ListView和GridView中加载图片的总结(二)
- 图片加载库之缓存策略和加载策略
- 加载图片(省缓存)
- Android图片加载框架之Picasso非常好的图片加载缓存库
- Android图片缓存之Glide进阶(四)
- android 之图片异步加载,带缓存。
- NGUI (四)加载图片
- android 异步图片加载四之handler+线程池+消息队列模式+缓存
- Android异步加载学习笔记之四:利用缓存优化网络加载图片及ListView加载优化
- Android有效加载图片 之 图片缓存技术
- 图片双缓存加载
- EGOImageView图片缓存加载
- 为什么Intent传递值会失败?
- 2.24
- LEETCODE-Power of Two
- 中国剩余定理
- lintcode-子集17
- 从零开始编写图片加载库(四)之图片缓存MemoryCache
- POJ 1840:Eqs 哈希求解五元方程
- 标准库bind函数中使用占位符placeholders
- 带有进度显示的文件拷贝模块
- python将对象写入文件,以及从文件中读取对象
- 夜深的窗,星空的床,外星的女人,想象的放荡,逻辑的流氓,懒了,忘了,跑了。。。
- 1035. 插入与归并(25)
- 泛型委托 Predicate/Func/Action
- CF round 314 E