Android中圖片的三級緩存機(jī)制
我們不能每次加載圖片的時候都讓用戶從網(wǎng)絡(luò)上下載,這樣不僅浪費(fèi)流量又會影響用戶體驗(yàn),所以Android中引入了圖片的緩存這一操作機(jī)制。
原理:
首先根據(jù)圖片的網(wǎng)絡(luò)地址在網(wǎng)絡(luò)上下載圖片,將圖片先緩存到內(nèi)存緩存中,緩存到強(qiáng)引用中 也就是LruCache中。如果強(qiáng)引用中空間不足,就會將較早存儲的圖片對象驅(qū)逐到軟引用(softReference)中存儲,然后將圖片緩存到文件(內(nèi)部存儲外部存儲)中;讀取圖片的時候,先讀取內(nèi)存緩存,判斷強(qiáng)引用中是否存在圖片,如果強(qiáng)引用中存在,則直接讀取,如果強(qiáng)引用中不存在,則判斷軟引用中是否存在,如果軟引用中存在,則將軟引用中的圖片添加到強(qiáng)引用中并且刪除軟引用中的數(shù)據(jù),如果軟引用中不存在,則讀取文件存儲,如果文件存儲不存在,則網(wǎng)絡(luò)加載。
下載: 網(wǎng)絡(luò)--內(nèi)存--文件
讀?。?內(nèi)存--強(qiáng)引用--軟引用--文件--網(wǎng)絡(luò)
也就是這樣的一個過程,下面用一個簡單地demo來演示一下圖片你的三級緩存,此demo中只有一個界面,界面上一個ImageView用來顯示圖片,一個按鈕用來點(diǎn)擊的時候加載圖片。布局如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/iv_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:layout_centerInParent="true"/> <Button android:id="@+id/btn_download" android:layout_below="@+id/iv_img" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加載圖片"/> </RelativeLayout>
因?yàn)橐獜木W(wǎng)絡(luò)下載數(shù)據(jù),還要存儲到本地sd卡中,所以不要忘了為程序添加網(wǎng)絡(luò)訪問的權(quán)限、網(wǎng)絡(luò)狀態(tài)訪問的權(quán)限和向外部存儲設(shè)備寫內(nèi)容的權(quán)限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
接著,創(chuàng)建一個 HttpUtils 工具類用于訪問網(wǎng)絡(luò),代碼如下:
package com.yztc.lx.cashimg; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; /**網(wǎng)絡(luò)訪問工具類 * Created by Lx on 2016/8/19. */ public class HttpUtils { /** * 判斷網(wǎng)絡(luò)連接是否通暢 * @param mContext * @return */ public static boolean isNetConn(Context mContext) { ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null) { return info.isConnected(); } else { return false; } } /** * 根據(jù)path下載網(wǎng)絡(luò)上的數(shù)據(jù) * @param path 路徑 * @return 返回下載內(nèi)容的byte數(shù)據(jù)形式 */ public static byte[] getDateFromNet(String path) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setDoInput(true); conn.connect(); if (conn.getResponseCode()==200) { InputStream is = conn.getInputStream(); byte b[] = new byte[1024]; int len; while ((len=is.read(b))!=-1) { baos.write(b, 0, len); } return baos.toByteArray(); } } catch (IOException e) { e.printStackTrace(); } return baos.toByteArray(); } }
還有操作外部存儲的工具類:
package com.yztc.lx.cashimg; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * Created by Lx on 2016/8/20. */ public class ExternalStorageUtils { /** * 將傳遞過來的圖片byte數(shù)組存儲到sd卡中 * @param imgName 圖片的名字 * @param buff byte數(shù)組 * @return 返回是否存儲成功 */ public static boolean storeToSDRoot(String imgName, byte buff[]) { boolean b = false; String basePath = Environment.getExternalStorageDirectory().getAbsolutePath(); File file = new File(basePath, imgName); try { FileOutputStream fos = new FileOutputStream(file); fos.write(buff); fos.close(); b = true; } catch (IOException e) { e.printStackTrace(); } return b; } /** * 從本地內(nèi)存中根據(jù)圖片名字獲取圖片 * @param imgName 圖片名字 * @return 返回圖片的Bitmap格式 */ public static Bitmap getImgFromSDRoot(String imgName) { Bitmap bitmap = null; String basePath = Environment.getExternalStorageDirectory().getAbsolutePath(); File file = new File(basePath, imgName); try { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte b[] = new byte[1024]; int len; while ((len = fis.read(b)) != -1) { baos.write(b, 0, len); } byte buff[] = baos.toByteArray(); if (buff != null && buff.length != 0) { bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length); } } catch (IOException e) { e.printStackTrace(); } return bitmap; } }
本例中將圖片默認(rèn)存在了sd卡根目錄中。
然后是最主要的主函數(shù)了:
package com.yztc.lx.cashimg; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.util.LruCache; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import java.lang.ref.SoftReference; import java.util.LinkedHashMap; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_download; private ImageView iv_img; private MyLruCache myLruCache; private LinkedHashMap<String, SoftReference<Bitmap>> cashMap = new LinkedHashMap<>(); private static final String TAG = "MainActivity"; private String imgPath = "http://www.3dmgame.com/UploadFiles/201212/Medium_20121217143424221.jpg"; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { Bitmap bitmap = (Bitmap) msg.obj; iv_img.setImageBitmap(bitmap); Toast.makeText(MainActivity.this, "從網(wǎng)絡(luò)上下載圖片", Toast.LENGTH_SHORT).show(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); int totalMemory = (int) Runtime.getRuntime().maxMemory(); int size = totalMemory / 8; myLruCache = new MyLruCache(size); btn_download.setOnClickListener(this); } private void initView() { btn_download = (Button) findViewById(R.id.btn_download); iv_img = (ImageView) findViewById(R.id.iv_img); } @Override public void onClick(View v) { Bitmap b = getImgCache(); if (b != null) { iv_img.setImageBitmap(b); } else { new Thread(new Runnable() { @Override public void run() { if (HttpUtils.isNetConn(MainActivity.this)) { byte b[] = HttpUtils.getDateFromNet(imgPath); if (b != null && b.length != 0) { Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length); Message msg = Message.obtain(); msg.obj = bitmap; handler.sendMessage(msg); myLruCache.put(imgPath, bitmap); Log.d(TAG, "run: " + "緩存到強(qiáng)引用中成功"); boolean bl = ExternalStorageUtils.storeToSDRoot("haha.jpg", b); if (bl) { Log.d(TAG, "run: " + "緩存到本地內(nèi)存成功"); } else { Log.d(TAG, "run: " + "緩存到本地內(nèi)存失敗"); } } else { Toast.makeText(MainActivity.this, "下載失?。?, Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(MainActivity.this, "請檢查你的網(wǎng)絡(luò)!", Toast.LENGTH_SHORT).show(); } } }).start(); } } /** * 從緩存中獲取圖片 * * @return 返回獲取到的Bitmap */ public Bitmap getImgCache() { Bitmap bitmap = myLruCache.get(imgPath); if (bitmap != null) { Log.d(TAG, "getImgCache: " + "從LruCache獲取圖片"); } else { SoftReference<Bitmap> sr = cashMap.get(imgPath); if (sr != null) { bitmap = sr.get(); myLruCache.put(imgPath, bitmap); cashMap.remove(imgPath); Log.d(TAG, "getImgCache: " + "從軟引用獲取圖片"); } else { bitmap = ExternalStorageUtils.getImgFromSDRoot("haha.jpg"); Log.d(TAG, "getImgCache: " + "從外部存儲獲取圖片"); } } return bitmap; } /** * 自定義一個方法繼承系統(tǒng)的LruCache方法 */ public class MyLruCache extends LruCache<String, Bitmap> { /** * 必須重寫的構(gòu)造函數(shù),定義強(qiáng)引用緩存區(qū)的大小 * @param maxSize for caches that do not override {@link #sizeOf}, this is * the maximum number of entries in the cache. For all other caches, * this is the maximum sum of the sizes of the entries in this cache. */ public MyLruCache(int maxSize) { super(maxSize); } //返回每個圖片的大小 @Override protected int sizeOf(String key, Bitmap value) { //獲取當(dāng)前變量每行的字節(jié)數(shù)和行高度(基本是固定寫法,記不住給我背?。? return value.getRowBytes() * value.getHeight(); } /** * 當(dāng)LruCache中的數(shù)據(jù)被驅(qū)逐或是移除時回調(diào)的函數(shù) * * @param evicted 當(dāng)LruCache中的數(shù)據(jù)被驅(qū)逐用來給新的value倒出空間的時候變化 * @param key 用來標(biāo)示對象的鍵,一般put的時候傳入圖片的url地址 * @param oldValue 之前存儲的舊的對象 * @param newValue 存儲的新的對象 */ @Override protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) { if (evicted) { /** * 將舊的值存到軟引用中,因?yàn)閺?qiáng)引用中可能有多個值被驅(qū)逐, * 所以創(chuàng)建一個LinkedHashMap<String, SoftReference<Bitmap>>來存儲軟引用 * 基本也是固定寫法 */ SoftReference<Bitmap> softReference = new SoftReference<Bitmap>(oldValue); cashMap.put(key, softReference); } } } }
基本的思路都在代碼注釋中寫的很詳細(xì)了,主要就是要自定義一個類,來繼承系統(tǒng)的LruCache,實(shí)現(xiàn)其中的兩個主要的方法sizeOf()和entryRemoved(),還有就是必須重寫它的構(gòu)造函數(shù)。
以上所述是小編給大家介紹的Android中圖片的三級緩存機(jī)制的全部敘述,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的,在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android圖片三級緩存開發(fā)
- 淺談Android 中圖片的三級緩存策略
- Android圖片三級緩存的原理及其實(shí)現(xiàn)
- Android中Rxjava實(shí)現(xiàn)三級緩存的兩種方式
- 詳解Android 圖片的三級緩存及圖片壓縮
- Android圖片三級緩存策略(網(wǎng)絡(luò)、本地、內(nèi)存緩存)
- Android使用緩存機(jī)制實(shí)現(xiàn)文件下載及異步請求圖片加三級緩存
- Android實(shí)現(xiàn)圖片異步請求加三級緩存
- android中圖片的三級緩存cache策略(內(nèi)存/文件/網(wǎng)絡(luò))
- Android三級緩存原理講解
相關(guān)文章
Android Jetpack Compose實(shí)現(xiàn)列表吸頂效果
安卓傳統(tǒng)的Recyclerview打造懸浮頭部StickyHeader的吸頂效果,十分麻煩,而在Compose中就簡單多了。因此,本文將采用Jetpack Compose實(shí)現(xiàn)列表吸頂效果,需要的可以參考一下2022-02-02android實(shí)現(xiàn)倒計(jì)時動態(tài)圈
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)倒計(jì)時動態(tài)圈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-01-01Android模擬實(shí)現(xiàn)華為系統(tǒng)升級進(jìn)度條
這篇文章主要介紹了如何通過Android模擬實(shí)現(xiàn)華為在系統(tǒng)升級時顯示的進(jìn)度條。文中的實(shí)現(xiàn)過程講解詳細(xì),感興趣的小伙伴可以動手試一試2022-01-01Android App啟動圖啟動界面(Splash)的簡單實(shí)現(xiàn)代碼
這篇文章主要介紹了Android App啟動圖啟動界面(Splash)的簡單實(shí)現(xiàn)代碼,本文通過實(shí)例圖文詳解相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05activity全屏實(shí)現(xiàn)沉浸式效果,并且單獨(dú)觸摸不會彈出虛擬按鍵的方法
今天小編就為大家分享一篇activity全屏實(shí)現(xiàn)沉浸式效果,并且單獨(dú)觸摸不會彈出虛擬按鍵的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android讀取properties配置文件的實(shí)例詳解
這篇文章主要介紹了Android讀取properties配置文件的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09詳解Android中Handler的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Android中Handler的實(shí)現(xiàn)原理,本文深入分析 Android 的消息處理機(jī)制,了解 Handler 的工作原理,感興趣的小伙伴們可以參考一下2016-04-04Flutter?Ping檢查服務(wù)器通訊信號強(qiáng)度實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了Flutter?Ping檢查服務(wù)器通訊信號強(qiáng)度實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06