Android中圖片的三級(jí)緩存機(jī)制
我們不能每次加載圖片的時(shí)候都讓用戶從網(wǎng)絡(luò)上下載,這樣不僅浪費(fèi)流量又會(huì)影響用戶體驗(yàn),所以Android中引入了圖片的緩存這一操作機(jī)制。
原理:
首先根據(jù)圖片的網(wǎng)絡(luò)地址在網(wǎng)絡(luò)上下載圖片,將圖片先緩存到內(nèi)存緩存中,緩存到強(qiáng)引用中 也就是LruCache中。如果強(qiáng)引用中空間不足,就會(huì)將較早存儲(chǔ)的圖片對(duì)象驅(qū)逐到軟引用(softReference)中存儲(chǔ),然后將圖片緩存到文件(內(nèi)部存儲(chǔ)外部存儲(chǔ))中;讀取圖片的時(shí)候,先讀取內(nèi)存緩存,判斷強(qiáng)引用中是否存在圖片,如果強(qiáng)引用中存在,則直接讀取,如果強(qiáng)引用中不存在,則判斷軟引用中是否存在,如果軟引用中存在,則將軟引用中的圖片添加到強(qiáng)引用中并且刪除軟引用中的數(shù)據(jù),如果軟引用中不存在,則讀取文件存儲(chǔ),如果文件存儲(chǔ)不存在,則網(wǎng)絡(luò)加載。
下載: 網(wǎng)絡(luò)--內(nèi)存--文件
讀?。?內(nèi)存--強(qiáng)引用--軟引用--文件--網(wǎng)絡(luò)
也就是這樣的一個(gè)過程,下面用一個(gè)簡(jiǎn)單地demo來演示一下圖片你的三級(jí)緩存,此demo中只有一個(gè)界面,界面上一個(gè)ImageView用來顯示圖片,一個(gè)按鈕用來點(diǎn)擊的時(shí)候加載圖片。布局如下:
<?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ù),還要存儲(chǔ)到本地sd卡中,所以不要忘了為程序添加網(wǎng)絡(luò)訪問的權(quán)限、網(wǎng)絡(luò)狀態(tài)訪問的權(quán)限和向外部存儲(chǔ)設(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)建一個(gè) 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();
}
}
還有操作外部存儲(chǔ)的工具類:
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ù)組存儲(chǔ)到sd卡中
* @param imgName 圖片的名字
* @param buff byte數(shù)組
* @return 返回是否存儲(chǔ)成功
*/
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, "請(qǐng)檢查你的網(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: " + "從外部存儲(chǔ)獲取圖片");
}
}
return bitmap;
}
/**
* 自定義一個(gè)方法繼承系統(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);
}
//返回每個(gè)圖片的大小
@Override
protected int sizeOf(String key, Bitmap value) {
//獲取當(dāng)前變量每行的字節(jié)數(shù)和行高度(基本是固定寫法,記不住給我背?。?
return value.getRowBytes() * value.getHeight();
}
/**
* 當(dāng)LruCache中的數(shù)據(jù)被驅(qū)逐或是移除時(shí)回調(diào)的函數(shù)
*
* @param evicted 當(dāng)LruCache中的數(shù)據(jù)被驅(qū)逐用來給新的value倒出空間的時(shí)候變化
* @param key 用來標(biāo)示對(duì)象的鍵,一般put的時(shí)候傳入圖片的url地址
* @param oldValue 之前存儲(chǔ)的舊的對(duì)象
* @param newValue 存儲(chǔ)的新的對(duì)象
*/
@Override
protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
if (evicted) {
/**
* 將舊的值存到軟引用中,因?yàn)閺?qiáng)引用中可能有多個(gè)值被驅(qū)逐,
* 所以創(chuàng)建一個(gè)LinkedHashMap<String, SoftReference<Bitmap>>來存儲(chǔ)軟引用
* 基本也是固定寫法
*/
SoftReference<Bitmap> softReference = new SoftReference<Bitmap>(oldValue);
cashMap.put(key, softReference);
}
}
}
}
基本的思路都在代碼注釋中寫的很詳細(xì)了,主要就是要自定義一個(gè)類,來繼承系統(tǒng)的LruCache,實(shí)現(xiàn)其中的兩個(gè)主要的方法sizeOf()和entryRemoved(),還有就是必須重寫它的構(gòu)造函數(shù)。
以上所述是小編給大家介紹的Android中圖片的三級(jí)緩存機(jī)制的全部敘述,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android圖片三級(jí)緩存開發(fā)
- 淺談Android 中圖片的三級(jí)緩存策略
- Android圖片三級(jí)緩存的原理及其實(shí)現(xiàn)
- Android中Rxjava實(shí)現(xiàn)三級(jí)緩存的兩種方式
- 詳解Android 圖片的三級(jí)緩存及圖片壓縮
- Android圖片三級(jí)緩存策略(網(wǎng)絡(luò)、本地、內(nèi)存緩存)
- Android使用緩存機(jī)制實(shí)現(xiàn)文件下載及異步請(qǐng)求圖片加三級(jí)緩存
- Android實(shí)現(xiàn)圖片異步請(qǐng)求加三級(jí)緩存
- android中圖片的三級(jí)緩存cache策略(內(nèi)存/文件/網(wǎng)絡(luò))
- Android三級(jí)緩存原理講解
相關(guān)文章
Android自定義View實(shí)現(xiàn)廣告信息上下滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)廣告信息上下滾動(dòng)的具體代碼,感興趣的小伙伴們可以參考一下2016-05-05
Android 開發(fā)實(shí)現(xiàn)EditText 光標(biāo)居右顯示
這篇文章主要介紹了Android 開發(fā)實(shí)現(xiàn)EditText 光標(biāo)居右顯示的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android 實(shí)現(xiàn)定時(shí)任務(wù)的過程詳解
這篇文章主要介紹了Android 定時(shí)任務(wù)過程詳解的相關(guān)資料,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
OnSharedPreferenceChangeListener詳解及出現(xiàn)不觸發(fā)解決辦法
本文主要介紹 Android OnSharedPreferenceChangeListener的知識(shí),在Android應(yīng)用開發(fā)過程中會(huì)遇到監(jiān)聽器不觸發(fā)事件問題,這里介紹了相應(yīng)的解決辦法2016-08-08
android實(shí)現(xiàn)菜單三級(jí)樹效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)菜單三級(jí)樹效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android 獲取應(yīng)用簽名的實(shí)現(xiàn)
本文主要講下在android中如何獲取應(yīng)用簽名,也方便平時(shí)用來區(qū)分一個(gè)應(yīng)用是不是原包應(yīng)用,具有一定的參考價(jià)值,感興趣的可以了解一下2016-02-02
Android自定義密碼樣式 黑點(diǎn)轉(zhuǎn)換成特殊字符
這篇文章主要為大家詳細(xì)介紹了Android自定義密碼樣式的制作方法,黑點(diǎn)換成¥、%等特殊字符,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

