首先比較下各大圖片庫(引用張濤大神的話)
Picasso :之所以說和Square的網絡庫一起能發揮最大作用,是因爲Picasso可以選擇將網絡請求的緩存部分交給了 okhttp 去實現
Glide:模仿了Picasso的API,而且在他的基礎上加了很多的擴展(比如gif等支持)
他們兩者最大的區別在於默認的設置不同
Glide默認的Bitmap格式是RGB_565 ,比Picasso默認的ARGB_8888格式的內存開銷要小一半
FaceBook的圖片加載框架Fresco:最大的優勢在於5.0以下(最低2.3)的bitmap加載。
在5.0以下系統,Fresco將圖片放到一個特別的內存區域(Ashmem區)。
當然,在圖片不顯示的時候,佔用的內存會自動被釋放。這會使得APP更加流暢,減少因圖片內存佔用而引發的OOM。
爲什麼說是5.0以下,因爲在5.0以後系統默認就是存儲在Ashmem(愛西門)區了。
最後,簡單粗暴的一句話概括:
Picasso所能實現的功能,Glide都能做,無非是所需的設置不同。但是Picasso體積比起Glide小太多如果項目中網絡請求本身用的就是okhttp或者retrofit(本質還是okhttp),那麼建議用Picasso,體積會小很多(Square全家桶的幹活)。
Glide的好處是大型的圖片流,比如gif、Video,如果你們是做美拍、愛拍這種視頻類應用,建議使用。
Fresco在5.0以下的內存優化非常好,代價就是體積也非常的大,按體積算Fresco>Glide>Picasso不過在使用起來也有些不便(小建議:他只能用內置的一個ImageView來實現這些功能,用起來比較麻煩,我們通常是根據Fresco自己改改,直接使用他的Bitmap層)
下面具體講解圖片三級緩存是哪三級?(Picasso 和 Glide 和 Fresco 基本也是這 三級)
原文:https://www.cnblogs.com/RabbitLx/p/5792268.html
我們不能每次加載圖片的時候都讓用戶從網絡上下載,這樣不僅浪費流量又會影響用戶體驗,所以Android中引入了圖片的緩存這一操作機制。
原理:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 <ImageView 7 android:id="@+id/iv_img" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:src="@mipmap/ic_launcher" 11 android:layout_centerInParent="true"/> 12 <Button 13 android:id="@+id/btn_download" 14 android:layout_below="@+id/iv_img" 15 android:layout_centerHorizontal="true" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:text="加載圖片"/> 19 20 </RelativeLayout>
因爲要從網絡下載數據,還要存儲到本地sd卡中,所以不要忘了爲程序添加網絡訪問的權限、網絡狀態訪問的權限和向外部存儲設備寫內容的權限:
1 <uses-permission android:name="android.permission.INTERNET" /> 2 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 3 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
接着,創建一個 HttpUtils 工具類用於訪問網絡,代碼如下:
1 package com.yztc.lx.cashimg; 2 3 import android.content.Context; 4 import android.net.ConnectivityManager; 5 import android.net.NetworkInfo; 6 7 import java.io.ByteArrayOutputStream; 8 import java.io.IOException; 9 import java.io.InputStream; 10 import java.net.HttpURLConnection; 11 import java.net.URL; 12 13 /**網絡訪問工具類 14 * Created by Lx on 2016/8/19. 15 */ 16 17 public class HttpUtils { 18 /** 19 * 判斷網絡連接是否通暢 20 * @param mContext 21 * @return 22 */ 23 public static boolean isNetConn(Context mContext) { 24 ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); 25 NetworkInfo info = manager.getActiveNetworkInfo(); 26 if (info != null) { 27 return info.isConnected(); 28 } else { 29 return false; 30 } 31 } 32 33 /** 34 * 根據path下載網絡上的數據 35 * @param path 路徑 36 * @return 返回下載內容的byte數據形式 37 */ 38 public static byte[] getDateFromNet(String path) { 39 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 40 try { 41 URL url = new URL(path); 42 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 43 conn.setRequestMethod("GET"); 44 conn.setConnectTimeout(5000); 45 conn.setDoInput(true); 46 conn.connect(); 47 if (conn.getResponseCode()==200) { 48 InputStream is = conn.getInputStream(); 49 byte b[] = new byte[1024]; 50 int len; 51 while ((len=is.read(b))!=-1) { 52 baos.write(b, 0, len); 53 } 54 return baos.toByteArray(); 55 } 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 return baos.toByteArray(); 60 } 61 62 }
還有操作外部存儲的工具類:
1 package com.yztc.lx.cashimg; 2 3 import android.graphics.Bitmap; 4 import android.graphics.BitmapFactory; 5 import android.os.Environment; 6 7 import java.io.ByteArrayOutputStream; 8 import java.io.File; 9 import java.io.FileInputStream; 10 import java.io.FileOutputStream; 11 import java.io.IOException; 12 13 /** 14 * Created by Lx on 2016/8/20. 15 */ 16 17 public class ExternalStorageUtils { 18 /** 19 * 將傳遞過來的圖片byte數組存儲到sd卡中 20 * @param imgName 圖片的名字 21 * @param buff byte數組 22 * @return 返回是否存儲成功 23 */ 24 public static boolean storeToSDRoot(String imgName, byte buff[]) { 25 boolean b = false; 26 String basePath = Environment.getExternalStorageDirectory().getAbsolutePath(); 27 File file = new File(basePath, imgName); 28 try { 29 FileOutputStream fos = new FileOutputStream(file); 30 fos.write(buff); 31 fos.close(); 32 b = true; 33 } catch (IOException e) { 34 e.printStackTrace(); 35 } 36 return b; 37 } 38 39 /** 40 * 從本地內存中根據圖片名字獲取圖片 41 * @param imgName 圖片名字 42 * @return 返回圖片的Bitmap格式 43 */ 44 public static Bitmap getImgFromSDRoot(String imgName) { 45 Bitmap bitmap = null; 46 String basePath = Environment.getExternalStorageDirectory().getAbsolutePath(); 47 File file = new File(basePath, imgName); 48 try { 49 FileInputStream fis = new FileInputStream(file); 50 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 51 byte b[] = new byte[1024]; 52 int len; 53 while ((len = fis.read(b)) != -1) { 54 baos.write(b, 0, len); 55 } 56 byte buff[] = baos.toByteArray(); 57 if (buff != null && buff.length != 0) { 58 bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length); 59 } 60 } catch (IOException e) { 61 e.printStackTrace(); 62 } 63 return bitmap; 64 } 65 66 67 }
本例中將圖片默認存在了sd卡根目錄中。
然後是最主要的主函數了:
1 package com.yztc.lx.cashimg; 2 3 import android.graphics.Bitmap; 4 import android.graphics.BitmapFactory; 5 import android.os.Bundle; 6 import android.os.Handler; 7 import android.os.Message; 8 import android.support.v7.app.AppCompatActivity; 9 import android.util.Log; 10 import android.util.LruCache; 11 import android.view.View; 12 import android.widget.Button; 13 import android.widget.ImageView; 14 import android.widget.Toast; 15 16 import java.lang.ref.SoftReference; 17 import java.util.LinkedHashMap; 18 19 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 20 21 private Button btn_download; 22 private ImageView iv_img; 23 private MyLruCache myLruCache; 24 private LinkedHashMap<String, SoftReference<Bitmap>> cashMap = new LinkedHashMap<>(); 25 private static final String TAG = "MainActivity"; 26 private String imgPath = "http://www.3dmgame.com/UploadFiles/201212/Medium_20121217143424221.jpg"; 27 private Handler handler = new Handler() { 28 @Override 29 public void handleMessage(Message msg) { 30 Bitmap bitmap = (Bitmap) msg.obj; 31 iv_img.setImageBitmap(bitmap); 32 Toast.makeText(MainActivity.this, "從網絡上下載圖片", Toast.LENGTH_SHORT).show(); 33 } 34 }; 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.activity_main); 40 initView(); 41 int totalMemory = (int) Runtime.getRuntime().maxMemory(); 42 int size = totalMemory / 8; 43 myLruCache = new MyLruCache(size); 44 btn_download.setOnClickListener(this); 45 } 46 47 private void initView() { 48 btn_download = (Button) findViewById(R.id.btn_download); 49 iv_img = (ImageView) findViewById(R.id.iv_img); 50 } 51 52 @Override 53 public void onClick(View v) { 54 Bitmap b = getImgCache(); 55 if (b != null) { 56 iv_img.setImageBitmap(b); 57 } else { 58 new Thread(new Runnable() { 59 @Override 60 public void run() { 61 if (HttpUtils.isNetConn(MainActivity.this)) { 62 byte b[] = HttpUtils.getDateFromNet(imgPath); 63 if (b != null && b.length != 0) { 64 Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length); 65 Message msg = Message.obtain(); 66 msg.obj = bitmap; 67 handler.sendMessage(msg); 68 myLruCache.put(imgPath, bitmap); 69 Log.d(TAG, "run: " + "緩存到強引用中成功"); 70 boolean bl = ExternalStorageUtils.storeToSDRoot("haha.jpg", b); 71 if (bl) { 72 Log.d(TAG, "run: " + "緩存到本地內存成功"); 73 } else { 74 Log.d(TAG, "run: " + "緩存到本地內存失敗"); 75 } 76 } else { 77 Toast.makeText(MainActivity.this, "下載失敗!", Toast.LENGTH_SHORT).show(); 78 } 79 80 } else { 81 Toast.makeText(MainActivity.this, "請檢查你的網絡!", Toast.LENGTH_SHORT).show(); 82 } 83 } 84 }).start(); 85 } 86 } 87 88 /** 89 * 從緩存中獲取圖片 90 * 91 * @return 返回獲取到的Bitmap 92 */ 93 public Bitmap getImgCache() { 94 Bitmap bitmap = myLruCache.get(imgPath); 95 if (bitmap != null) { 96 Log.d(TAG, "getImgCache: " + "從LruCache獲取圖片"); 97 } else { 98 SoftReference<Bitmap> sr = cashMap.get(imgPath); 99 if (sr != null) { 100 bitmap = sr.get(); 101 myLruCache.put(imgPath, bitmap); 102 cashMap.remove(imgPath); 103 Log.d(TAG, "getImgCache: " + "從軟引用獲取圖片"); 104 } else { 105 bitmap = ExternalStorageUtils.getImgFromSDRoot("haha.jpg"); 106 Log.d(TAG, "getImgCache: " + "從外部存儲獲取圖片"); 107 } 108 } 109 return bitmap; 110 } 111 112 /** 113 * 自定義一個方法繼承系統的LruCache方法 114 */ 115 public class MyLruCache extends LruCache<String, Bitmap> { 116 117 /** 118 * 必須重寫的構造函數,定義強引用緩存區的大小 119 * @param maxSize for caches that do not override {@link #sizeOf}, this is 120 * the maximum number of entries in the cache. For all other caches, 121 * this is the maximum sum of the sizes of the entries in this cache. 122 */ 123 public MyLruCache(int maxSize) { 124 super(maxSize); 125 } 126 127 //返回每個圖片的大小 128 @Override 129 protected int sizeOf(String key, Bitmap value) { 130 //獲取當前變量每行的字節數和行高度(基本是固定寫法,記不住給我背!) 131 return value.getRowBytes() * value.getHeight(); 132 } 133 134 /** 135 * 當LruCache中的數據被驅逐或是移除時回調的函數 136 * 137 * @param evicted 當LruCache中的數據被驅逐用來給新的value倒出空間的時候變化 138 * @param key 用來標示對象的鍵,一般put的時候傳入圖片的url地址 139 * @param oldValue 之前存儲的舊的對象 140 * @param newValue 存儲的新的對象 141 */ 142 @Override 143 protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) { 144 if (evicted) { 145 /** 146 * 將舊的值存到軟引用中,因爲強引用中可能有多個值被驅逐, 147 * 所以創建一個LinkedHashMap<String, SoftReference<Bitmap>>來存儲軟引用 148 * 基本也是固定寫法 149 */ 150 SoftReference<Bitmap> softReference = new SoftReference<Bitmap>(oldValue); 151 cashMap.put(key, softReference); 152 } 153 } 154 } 155 }
基本的思路都在代碼註釋中寫的很詳細了,主要就是要自定義一個類,來繼承系統的LruCache,實現其中的兩個主要的方法sizeOf()和entryRemoved(),還有就是必須重寫它的構造函數。