• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Ocean123123
博客园    首页    新随笔    联系   管理    订阅  订阅

LruCache缓存bitmap(三)

应用在网络连接上,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);
        }
    }
}

 

posted @ 2019-06-05 18:56  Ocean123123  阅读(152)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3