android: 缓存异步加载Bitmap
来源:互联网 发布:人工智能的弊端论文 编辑:程序博客网 时间:2024/05/18 00:41
step1:创建内存缓存
//采用内存缓存,速度快,占用内存
public class MemoryCache {
private static LruCache<String, Bitmap> cache;
private MemoryCache(){};
public static LruCache<String, Bitmap> getInstance(){
if(cache == null){
final int _maxMemory = (int)(Runtime.getRuntime().maxMemory()/1024);
final int _cacheSize = _maxMemory/8;
cache = new LruCache<String, Bitmap>(_cacheSize);
}
return cache;
}
public static void addBitmapToMemoryCache(String key,Bitmap bmp){
if(cache.get(key) == null)
cache.put(key, bmp);
}
public static Bitmap getBitmapFromCache(String key){
return cache.get(key);
}
}
step2:压缩Bitmap
public class BitmapCompress {
public static Bitmap Comperss(Resources res,int resId,int requestWidth,int requestHeight){
BitmapFactory.Options _options = new BitmapFactory.Options();
_options.inJustDecodeBounds = true;//不加载进内存
BitmapFactory.decodeResource(res, resId, _options);
int outWidth = _options.outWidth;
int outHeight = _options.outHeight;
int inSampleSize =1;
//计算压缩比例
while(outWidth/requestWidth >inSampleSize
&&outHeight/requestHeight>inSampleSize){
inSampleSize*=2;
}
_options.inSampleSize = inSampleSize;
//加载进内存
_options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, _options);
}
}
step3:异步加载Bitmap
public class LoadImageTask extends AsyncTask<Integer,Integer,Bitmap>{
private final WeakReference<ImageView> mImageViewReference;
private Context mContext;
public LoadImageTask(Context pContext,ImageView pImageView){
mContext = pContext;
mImageViewReference = new WeakReference<ImageView>(pImageView);
}
@Override
protected Bitmap doInBackground(Integer... params) {
Bitmap _bitmap = BitmapCompress.Comperss(mContext.getResources(), params[0], 200, 200);
//将_bitmap加入缓存中
MemoryCache.getInstance().put(String.valueOf(params[0]), _bitmap);
return _bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if(mImageViewReference != null && result != null){
ImageView _imageview = mImageViewReference.get();
if(_imageview != null)
_imageview.setImageBitmap(result);
}
}
}
- android: 缓存异步加载Bitmap
- Bitmap二次采样,Android中的缓存处理及异步加载图片类的封装
- Android异步加载全解析之Bitmap
- Android 位图加载 采样 缓存设计 Bitmap
- 用心学Android之Bitmap缓存编,可为ListView等控件提供异步加载图片解决方案
- (源码分析)Android-Universal-Image-Loader (图片异步加载缓存库)对Bitmap的优化处理
- 异步加载bitmap图片
- Bitmap 图片异步加载
- 异步缓存,异步加载
- BitMap加载、压缩、缓存
- android异步图片加载中的图片缓存
- android 之图片异步加载,带缓存。
- android 图片缓存 异步加载 简要介绍
- Android异步加载网络图片 + 双缓存
- android 异步加载网络图片缓存机制
- Android Bitmap高效加载与LruCache内存缓存
- Android 整理BitMap的加载和图片缓存
- Bitmap异步加载网络图片
- 快速排序 shell实现
- ssh_exchange_identification: Connection closed by remote
- 表的基本操作
- KindEditor图片上传URL路径处理
- i++和++i 和i+1的区别
- android: 缓存异步加载Bitmap
- 习题3-36
- js校验时间格式
- 关于Linux内核同时运行进程总数的讨论
- tyvj P1209 拦截导弹
- spi slave
- java类的成员初始化顺序和初始化块知识
- 使用volley时 onErrorResponse错误,如何查看详细的信息
- Linux下使用Shell脚本改变当前工作路径