国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

圖片的三級緩存

2019-11-09 16:47:35
字體:
供稿:網(wǎng)友

三級緩存基本原理

內(nèi)存緩存:速度很快,不浪費流量,優(yōu)先本地緩存:速度快,不浪費流量,其次網(wǎng)絡(luò)緩存:速度慢,浪費流量,最后

網(wǎng)絡(luò)緩存

思路:

從網(wǎng)絡(luò)下載圖片,通過AsyncTask開啟線程,創(chuàng)建HttpURLConnection對象,把輸入流轉(zhuǎn)成Bitmap

因為ListView圖片錯亂問題,需要給ImageView設(shè)置的Tag是圖片的url鏈接,展示圖片的時候,比較tag和url是否相等

代碼:/** * 網(wǎng)絡(luò)緩存工具類 */public class NetCacheUtils { PRivate static final String TAG = "NetCacheUtils"; public void getBitmapFromNet(ImageView imageView, String url) { //異步下載圖片 new BitmapTask(imageView, url).execute(imageView, url); } class BitmapTask extends AsyncTask<Object, Void, Bitmap> { private ImageView imageView; private String url; public BitmapTask(ImageView imageView, String url) { this.imageView = imageView; this.url = url; this.imageView.setTag(this.url); } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Bitmap doInBackground(Object... params) { //使用url下載圖片 Bitmap bitmap = download(url); return bitmap; } @Override protected void onPostExecute(Bitmap result) { //給ImageView設(shè)置圖片 //由于ListView的重用機制, // 導致某個item有可能展示它所重用的那個item的圖片,導致圖片錯亂 //解決方案:確保當前設(shè)置的圖片和當前顯示的imageView完全匹配 if (result != null) { String url = (String) imageView.getTag(); //判斷當前下載的圖片的url是否和ImageView的url一致,如果一致,說明完全匹配 if (this.url.equals(url)) { imageView.setImageBitmap(result); Log.i(TAG, "onPostExecute: 從網(wǎng)絡(luò)上下載圖片啦"); } } } } //下載圖片 private Bitmap download(String url) { HttpURLConnection conn = null; try { conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(6000);//設(shè)置連接超時 conn.setReadTimeout(6000);//設(shè)置讀取超時 conn.connect();//連接 int responseCode = conn.getResponseCode();//獲取返回碼 if (responseCode == 200) { InputStream in = conn.getInputStream();//獲取輸入流 //使用輸入流生成Bitmap對象 Bitmap bitmap = BitmapFactory.decodeStream(in); return bitmap; } } catch (Exception e) { e.printStackTrace(); }finally { if (conn != null) { conn.disconnect(); } } return null; }}

本地緩存

思路:

創(chuàng)建本地緩存工具,首先創(chuàng)建緩存目錄,圖片url通過md5加密作為文件名從網(wǎng)絡(luò)下載圖片后,保存圖片到本地展示圖片時,首先根據(jù)url判斷本地是否有緩存,如果有就讀取本地緩存,沒有就從網(wǎng)上下載

代碼: /** * 本地緩存工具類 */ public class LocalCacheUtils { //緩存文件夾 private String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + “/zhbj_cache/”;

//寫緩存 public void setLocalCache(String url, Bitmap bitmap) { //將圖片保存在本地文件中 File dir = new File(PATH); if (!dir.exists() || !dir.isDirectory()) { dir.mkdirs();//創(chuàng)建文件夾 } try { File cacheFile = new File(dir, MD5Encoder.encode(url)); //將圖片壓縮保存到本地,參數(shù)1:圖片的格式,參數(shù)2:壓縮比(0-100), 參數(shù)3:輸出流 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(cacheFile)); } catch (Exception e) { e.printStackTrace(); } } //讀緩存 public Bitmap getLocalCache(String url) { try { File cacheFile = new File(PATH, MD5Encoder.encode(url)); if (cacheFile.exists()) { //緩存存在 Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(cacheFile)); return bitmap; } } catch (Exception e) { e.printStackTrace(); } return null; } }

內(nèi)存緩存

思路:

創(chuàng)建HashMap,key是url,value是Bitmap從網(wǎng)絡(luò)下載圖片展示后,把bitmap存放到HashMap中,從本地緩存中讀取圖片展示后,把bitmap存放到HashMap中

展示圖片時,如果內(nèi)存緩存有bitmap,則不需要從本地或者網(wǎng)絡(luò)加載圖片

代碼: /** * 內(nèi)存緩存 */ public class MemoryCacheUtils { private Map<String, Bitmap> mHashMap = new HashMap<>(); //寫緩存 public void setMemoryCache(String url, Bitmap bitmap) { mHashMap.put(url, bitmap); } //讀緩存 public Bitmap getMemroyCache(String url) { return mHashMap.get(url); } }

四個引用

強引用,StrongReference,默認,垃圾回收器不回收軟引用,SoftReference,內(nèi)存不足時,垃圾回收器考慮回收弱引用,WeakReference,內(nèi)存不足時,垃圾回收器更會考慮回收虛引用,PhantomReference,內(nèi)存不足時,垃圾回收器最會考慮回收

思路: 1. 把bitmap用SoftReference包裝起來,然后再存放到map集合中 2. 讀取時先獲取SoftReference對象,再從軟引用對象中獲取bitmap

獲取虛擬機分配的最大內(nèi)存,默認16M long maxMemory = Runtime.getRuntime().maxMemory(); maxSize,內(nèi)存緩存上限 mLruCache = new LruCache<String, Bitmap>((int) (maxMemory/8)){ //返回單個對象占用內(nèi)存的大小 @Override protected int sizeOf(String key, Bitmap value) { int byteCount = value.getRowBytes() * value.getHeight(); return byteCount; } };
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大港区| 邵武市| 芦山县| 南召县| 上犹县| 平远县| 温泉县| 秦皇岛市| 中牟县| 古丈县| 浦北县| 灵川县| 北流市| 鹤峰县| 隆德县| 隆安县| 连江县| 阿拉善右旗| 宁远县| 依安县| 隆昌县| 普兰店市| 阿合奇县| 黎川县| 宁国市| 德保县| 含山县| 鄱阳县| 石家庄市| 灵寿县| 林口县| 景洪市| 花莲县| 娄底市| 阜城县| 玉林市| 屯留县| 瓦房店市| 灌南县| 抚远县| 青海省|