自己封装的 —— 三级缓存
来源:互联网 发布:proe散热模拟软件 编辑:程序博客网 时间:2024/06/05 10:18
布局
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
2:自己定义个工具类
public class BitmapUtils {
Context context;
//图片本地缓存路径
private final static String SDCARD_CACHE = Environment.getExternalStorageDirectory() + "/imagecache";
//图片存放文件夹
File fileDir = new File(SDCARD_CACHE);
private Map<String, SoftReference<Bitmap>> map = new HashMap<String, SoftReference<Bitmap>>();
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
ImageViewToBitmap imageViewToBitmap = (ImageViewToBitmap) msg.obj;
imageViewToBitmap.iv.setImageBitmap(imageViewToBitmap.bitmap);
break;
}
}
};
//构造方法
public BitmapUtils(Context context) {
this.context = context;
if (!fileDir.exists()) {
fileDir.mkdirs();
}
}
//加载图片的方法
public void display(ImageView iv, String url) {
//内存缓存
Bitmap bitmap = loadMemory(url);
if (bitmap != null) {
iv.setImageBitmap(bitmap);
} else {
//sdcard缓存
bitmap = loadSD(url);
if (bitmap != null) {
iv.setImageBitmap(bitmap);
} else {
//网络缓存
loadInternetImage(iv, url);
}
}
}
private Bitmap loadMemory(String url) {
SoftReference<Bitmap> value = map.get(url);
if (value != null) {
Bitmap bitmap = value.get();
return bitmap;
}
return null;
}
//获取本地图片
private Bitmap loadSD(String url) {
String name = getFileName(url);
File file = new File(fileDir, name);
if (file.exists()) {
//BitmapFactory选项
BitmapFactory.Options options = new BitmapFactory.Options();
//加载图片宽高
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(name, options);
//获取图片和手机屏幕宽高
int outWidth = options.outWidth;
int outHeight = options.outHeight;
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int widthPixels = displayMetrics.widthPixels;
int heightPixels = displayMetrics.heightPixels;
//图片跟手机屏幕进行比对
int scale = 0;
int scaleX = outWidth / widthPixels;
int scaleY = outHeight / heightPixels;
scale = scaleX > scaleY ? scaleX : scaleY;
if (scale == 0) {
scale = 1;
}
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
//内存缓存
SoftReference<Bitmap> value = new SoftReference<Bitmap>(bitmap);
map.put(name, value);
return bitmap;
}
return null;
}
//获取网络图片
private void loadInternetImage(ImageView iv, String url) {
//开子线程做耗时操作
new Thread(new DownloadImage(iv, url)).start();
}
private class DownloadImage implements Runnable {
ImageView iv;
String url;
private InputStream inputStream;
private FileOutputStream fos;
public DownloadImage(ImageView iv, String url) {
this.iv = iv;
this.url = url;
}
@Override
public void run() {
try {
HttpClient clent = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse execute = clent.execute(get);
if (execute.getStatusLine().getStatusCode() == 200) {
inputStream = execute.getEntity().getContent();
String name = getFileName(url);
File file = new File(fileDir, name);
fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
//sdcard缓存
Bitmap bitmap = loadSD(name);
//ImageView转换成Bitmap转换成Bitmap
ImageViewToBitmap ivtb = new ImageViewToBitmap(iv, bitmap);
Message message = Message.obtain(handler, 0, ivtb);
message.sendToTarget();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//获取图片的名称
private String getFileName(String url) {
return Md5Utils.encode(url) + ".jpg";
}
//ImageView转换成Bitmap转换成Bitmap
private class ImageViewToBitmap {
ImageView iv;
Bitmap bitmap;
public ImageViewToBitmap(ImageView iv, Bitmap bitmap) {
this.iv = iv;
this.bitmap = bitmap;
}
}
}
3:加密
public class Md5Utils {
public static String encode(String password){
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] result = digest.digest(password.getBytes());
StringBuffer sb = new StringBuffer();
for(byte b : result){
int number = (int)(b & 0xff) ;
String str = Integer.toHexString(number);
if(str.length()==1){
sb.append("0");
}
sb.append(str);
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
//can't reach
return "";
}
}
}
4:MainActivity 方法
public class MainActivity extends AppCompatActivity {
private String url = "http://d.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40fe871b36bad64034f79f019d4.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
ImageView iv = (ImageView) findViewById(R.id.iv);
//实例化图片加载框架
BitmapUtils bitmapUtils = new BitmapUtils(this);
bitmapUtils.display(iv, url);
}
}
- 自己封装的 —— 三级缓存
- Bitmap的三级缓存----自己封装BitmapLoader
- 图片的三级缓存封装为library
- 利用LruCache封装一个自带三级缓存的ImageLoader
- 自己实现图片三级缓存
- 使用RxJava自己构造一个三级缓存的实例
- 图片的三级缓存——学习笔记
- <Android>之三级缓存(及封装方法)
- GridView的三级缓存
- 图片的三级缓存
- Git的三级缓存
- 图片的三级缓存
- 图片的三级缓存
- 图片的三级缓存
- android的三级缓存
- 图片的三级缓存
- 图片的三级缓存
- 图片的三级 缓存
- 多线程GCD之dispatch_semaphore_t
- 手机短视频应用 --用户行为分析和优化指南
- appium自动化_登录apk代码
- Easyui dialog中嵌入iframe
- 在MathType编辑乘号的方法介绍
- 自己封装的 —— 三级缓存
- python scrapy 去重
- Lotus and Characters__HDU6011
- js跳转页面方法汇总。
- Android View的事件体系
- SpringBoot集成Swagger2
- hdu 4908 以k为中位数的子序列计数
- 安卓开发笔记——关于图片的三级缓存策略(内存LruCache+磁盘DiskLruCache+网络Volley)
- 八皇后计算