应用在网络连接上,onrestart后不会重新联网获取图片,省去了流量,

public class MainActivity extends AppCompatActivity {
ImageView imageView;
private LruCache<String, Bitmap> lruCache;
Bitmap bitmap; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image);
long maxMemory = Runtime.getRuntime().maxMemory();
int cacheSize = (int) (maxMemory / 8);
lruCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
} class BitmapThread extends Thread {
private String bitmapUrl; BitmapThread(String bitmapUrl) {
this.bitmapUrl = bitmapUrl;
} @Override
public void run() {
Log.i("名字", "run: " + Thread.currentThread().getName());
Bitmap bitmap3 = null;
HttpURLConnection connection = null;
InputStream inputStream = null;
try {
URL url = new URL(bitmapUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = connection.getInputStream();
bitmap3 = BitmapFactory.decodeStream(inputStream); } handler.obtainMessage(1, bitmap3).sendToTarget();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i("线程名字", "hanlder handleMessage: " + Thread.currentThread().getName());
switch (msg.what) {
case 1:
bitmap = (Bitmap) msg.obj;
imageView.setImageBitmap(bitmap);
lruCache.put("a", bitmap);
break;
}
}
}; @Override
protected void onStart() {
super.onStart();
Bitmap bitmap2 = lruCache.get("a");
if (bitmap2 == null) {
new BitmapThread("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3453332705,4236610029&fm=26&gp=0.jpg").start(); } else {
imageView.setImageBitmap(bitmap2);
}
}
}

LruCache缓存bitmap(三)的更多相关文章

  1. DiskLruCache和Lrucache缓存bitmap

    三级缓存,先在内存Lrucache中查找缓存,没有就去外存DiskLrucache中查找,再没有就下载,Lru不会自动删除,所以要设置最大缓存内存,后台运行Lrucache不会消失,关闭程序Diskl ...

  2. LruCache缓存bitmap(二)

    Lrucache缓存程序关闭缓存自动清除,所以要在onstart方法中调用,只要不关闭程序缓存就在,除以1024是以kb为单位 public class MainActivity extends Ap ...

  3. LruCache缓存bitmap(一)

    Lrucache是把图片缓存到内置sd卡,设置缓存容量为系统分配容量的八分之一,单位byte,超过缓存容量gc会自动回收不长使用的缓存.觉得lrucache就先map一样,放入键值对就行了,比较方便, ...

  4. 让App中加入LruCache缓存,轻松解决图片过多造成的OOM

    上次有过电话面试中问到Android中的缓存策略,当时模糊不清的回答,现在好好理一下吧. Android中一般情况下采取的缓存策略是使用二级缓存,即内存缓存+硬盘缓存->LruCache+Dis ...

  5. 让App中增加LruCache缓存,轻松解决图片过多造成的OOM

    上次有过电话面试中问到Android中的缓存策略,当时模糊不清的回答,如今好好理一下吧. Android中普通情况下採取的缓存策略是使用二级缓存.即内存缓存+硬盘缓存->LruCache+Dis ...

  6. Android笔记--Bitmap(三) 针对不用Android版本的位图管理

    Bitmap(三) | Android不同版本的相应操作 在不同的Android版本中.位图的存储方式是不同的. 1.小于等于 Android 2.2 (API level 8) 垃圾收集器回收内存时 ...

  7. Android源代码解析之(七)--&gt;LruCache缓存类

    转载请标明出处:一片枫叶的专栏 android开发过程中常常会用到缓存.如今主流的app中图片等资源的缓存策略通常是分两级.一个是内存级别的缓存,一个是磁盘级别的缓存. 作为android系统的维护者 ...

  8. Android-Universal-Image-Loader的缓存处理机制与使用 LruCache 缓存图片

    讲到缓存,平时流水线上的码农一定觉得这是一个高大上的东西.看过网上各种讲缓存原理的文章,总感觉那些文章讲的就是玩具,能用吗?这次我将带你一起看过UIL这个国内外大牛都追捧的图片缓存类库的缓存处理机制. ...

  9. LruCache缓存

    LruCache通常用于实现内存缓存,采用的缓存算法是LRU(Least Recently Used)即近期最少使用算法,其核心思想是:当缓存满的时候,会优先淘汰那些近期最少使用的缓存对象. 1.Lr ...

随机推荐

  1. java获取一天前的时间

    获取一天前的时间 Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); ...

  2. 剑指offer-递归和循环

    1. 斐波那契数列 解: 没啥好说的了,直接上高效的滚动迭代解法.矩阵解法和特征根解法这里不讨论了. class Solution: def Fibonacci(self, n): # write c ...

  3. 使用模拟退火算法优化 Hash 函数

    背景 现有个处理股票行情消息的系统,其架构如下: 由于数据量巨大,系统中启动了 15 个线程来消费行情消息.消息分配的策略较为简单:对 symbol 的 hashCode 取模,将消息分配给其中一个线 ...

  4. 整理requests和正则表达式爬取猫眼Top100中遇到的问题及解决方案

    最近看崔庆才老师的爬虫课程,第一个实战课程是requests和正则表达式爬取猫眼电影Top100榜单.虽然理解崔老师每一步代码的实现过程,但自己敲代码的时候还是遇到了不少问题: 问题1:获取respo ...

  5. Arduino 串行外设接口——W3Cschool

    来源:https://www.w3cschool.cn/arduino/arduino_serial_peripheral_interface.html Arduino 串行外设接口 由 drbear ...

  6. matlab中colormap

    来源:https://ww2.mathworks.cn/help/matlab/ref/colormap.html?searchHighlight=colormap&s_tid=doc_src ...

  7. MySQL计算月份间隔的函数

    要求忽视具体日期,即 2020-01-31 与 2020-02-01 的月份间隔为:1 -- 格式必须为: '%Y%m' SELECT PERIOD_DIFF("202008" , ...

  8. 设备通讯——RS232

    RS232的接口有两种--一种公头.一种母头,两种头的引脚是有区别的 MAX232电路图: 注意:串口通讯需要交叉接线.

  9. node_modules 文件夹需要管理员权限才能删除问题

    方法一:以管理员权限运行IDE ,然后在IDE里面删除该文件夹 方法二:以管理员身份运行cmd,使用命令行来删除该文件夹 找到要删除文件夹的位置,使用命令行 rmdir /s/q 文件夹位置 /s 是 ...

  10. java基础小程序—万年历

    package day02.xiangmu.wannianli; import java.util.Scanner; public class CalendarTest { public static ...