Android高效安全加載圖片的方法詳解

Android開發中消耗內存較多一般都是在圖像上面,下面這篇文章主要給大家介紹了關於Android如何高效安全加載圖片的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

1. 概述

在 Android 應用程序的設計中,幾乎不可避免地都需要加載和顯示圖片,由於不同的圖片在大小上千差萬別,有些圖片可能只需要幾十KB的內存空間,有些圖片卻需要佔用幾十MB的內存空間;或者一張圖片不需要佔用太多的內存,但是需要同時加載和顯示多張圖片。

在這些情況下,加載圖片都需要佔用大量的內存,而 Android系統分配給每個進程的內存空間是有限的,如果加載的圖片所需要的內存超過了限制,進程就會出現 OOM,即內存溢出。

本文針對加載大圖片或者一次加載多張圖片等兩種不同的場景,採用不同的加載方式,以儘量避免可能導致的內存溢出問題。

下面話不多說了,來一起看看詳細的介紹吧

2. 加載大圖片

有時一張圖片的加載和顯示就需要佔用大量的內存,例如圖片的大小是 2592x1936 ,同時採用的位圖配置是 ARGB_8888 ,其在內存中需要的大小是 2592x1936x4字節,大概是 19MB。僅僅加載這樣一張圖片就可能會超過進程的內存限制,進而導致內存溢出,所以在實際使用時肯定無法直接加載到內存中。

爲了避免內存溢出,根據不同的顯示需求,採取不同的加載方式:

  • 顯示一張圖片的全部內容:對原圖片進行 壓縮顯示。
  • 顯示一張圖片的部分內容:對原圖片進行 局部顯示。

2.1 圖片壓縮顯示

圖片的壓縮顯示指的是對原圖片進行長寬的壓縮,以減少圖片的內存佔用,使其能夠在應用上正常顯示,同時保證在加載和顯示過程中不會出現內存溢出的情況。

BitmapFactory 是一個創建Bitmap 對象的工具類,使用它可以利用不同來源的數據生成Bitamp對象,在創建過的過程中還可以對需要生成的對象進行不同的配置和控制,BitmapFactory的類聲明如下:

Creates Bitmap objects from various sources, including files, streams,and byte-arrays.

由於在加載圖片前,是無法提前預知圖片大小的,所以在實際加載前必須根據圖片的大小和當前進程的內存情況來決定是否需要對圖片進行壓縮,如果加載原圖片所需的內存空間已經超過了進程打算提供或可以提供的內存大小,就必須考慮壓縮圖片。

2.1.1 確定原圖片長寬

簡單來說,壓縮圖片就是對原圖的長寬按照一定的比例進行縮小,所以首先要確定原圖的長寬信息。爲了獲得圖片的長寬信息,利用 BitmapFactory.decodeResource(Resources res, int id, Options opts) 接口,其聲明如下:

 /**
 * Synonym for opening the given resource and calling
 * {@link #decodeResourceStream}.
 *
 * @param res The resources object containing the image data
 * @param id The resource id of the image data
 * @param opts null-ok; Options that control downsampling and whether the
 *  image should be completely decoded, or just is size returned.
 * @return The decoded bitmap, or null if the image data could not be
 *  decoded, or, if opts is non-null, if opts requested only the
 *  size be returned (in opts.outWidth and opts.outHeight)
 * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
 *  is {@link android.graphics.Bitmap.Config#HARDWARE}
 *  and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
 *  is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
 *  function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
 */
 public static Bitmap decodeResource(Resources res, int id, Options opts) {

通過這個函數聲明,可以看到通過這個接口可以得到圖片的長寬信息,同時由於返回 null並不申請內存空間,避免了不必要的內存申請。

爲了得到圖片的長寬信息,必須傳遞一個 Options 參數,其中的 inJustDecodeBounds 設置爲 true,其聲明如下:

 /**
 * If set to true, the decoder will return null (no bitmap), but
 * the <code>out...</code> fields will still be set, allowing the caller to
 * query the bitmap without having to allocate the memory for its pixels.
 */
 public boolean inJustDecodeBounds;

下面給出得到圖片長寬信息的示例代碼:

 BitmapFactory.Options options = new BitmapFactory.Options();
 // 指定在解析圖片文件時,僅僅解析邊緣信息而不創建 bitmap 對象。
 options.inJustDecodeBounds = true;
 // R.drawable.test 是使用的 2560x1920 的測試圖片資源文件。
 BitmapFactory.decodeResource(getResources(), R.drawable.test, options);
 int width = options.outWidth;
 int height = options.outHeight;
 Log.i(TAG, "width: " + width + ", height: " + height);

在實際測試中,得到的長寬信息如下:

    01-05 04:06:23.022 29836 29836 I Android_Test: width: 2560, height: 1920

2.1.2 確定目標壓縮比例

得知原圖片的長寬信息後,爲了能夠進行後續的壓縮操作,必須要先確定目標壓縮比例。所謂壓縮比例就是指要對原始的長寬進行的裁剪比例,如果如果原圖片是 2560x1920,採取的壓縮比例是 4,進行壓縮後的圖片是 640x480,最終大小是原圖片的1/16。

壓縮比例在 BitmapFactory.Options中對應的屬性是 inSampleSize,其聲明如下:

 /**
 * If set to a value > 1, requests the decoder to subsample the original
 * image, returning a smaller image to save memory. The sample size is
 * the number of pixels in either dimension that correspond to a single
 * pixel in the decoded bitmap. For example, inSampleSize == 4 returns
 * an image that is 1/4 the width/height of the original, and 1/16 the
 * number of pixels. Any value <= 1 is treated the same as 1. Note: the
 * decoder uses a final value based on powers of 2, any other value will
 * be rounded down to the nearest power of 2.
 */
 public int inSampleSize;

需要特別注意的是,inSampleSize 只能是 2的冪,如果傳入的值不滿足條件,解碼器會選擇一個和傳入值最節儉的2的冪;如果傳入的值小於 1,解碼器會直接使用1。

要確定最終的壓縮比例,首先要確定目標大小,即壓縮後的目標圖片的長寬信息,根據原始長寬和目標長寬來選擇一個最合適的壓縮比例。下面給出示例代碼:

 /**
 * @param originWidth the width of the origin bitmap
 * @param originHeight the height of the origin bitmap
 * @param desWidth the max width of the desired bitmap
 * @param desHeight the max height of the desired bitmap
 * @return the optimal sample size to make sure the size of bitmap is not more than the desired.
 */
 public static int calculateSampleSize(int originWidth, int originHeight, int desWidth, int desHeight) {
 int sampleSize = 1;
 int width = originWidth;
 int height = originHeight;
 while((width / sampleSize) > desWidth && (height / sampleSize) > desHeight) {
  sampleSize *= 2;
 }
 return sampleSize;
 }

需要注意的是這裏的desWidth和desHeight 是目標圖片的最大長寬值,而不是最終的大小,因爲通過這個方法確定的壓縮比例會保證最終的圖片長寬不大於目標值。

在實際測試中,把原圖片大小設置爲2560x1920,把目標圖片大小設置爲100x100:

 int sampleSize = BitmapCompressor.calculateSampleSize(2560, 1920, 100, 100);
 Log.i(TAG, "sampleSize: " + sampleSize);

測試結果如下:

    01-05 04:42:07.752  8835  8835 I Android_Test: sampleSize: 32

最終得到的壓縮比例是32,如果使用這個比例去壓縮2560x1920的圖片,最終得到80x60的圖片。

2.1.3 壓縮圖片

在前面兩部分,分別確定了原圖片的長寬信息和目標壓縮比例,其實確定原圖片的長寬也是爲了得到壓縮比例,既然已經得到的壓縮比較,就可以進行實際的壓縮操作了,只需要把得到的inSampleSize通過Options傳遞給BitmapFactory.decodeResource(Resources res, int id, Options opts)即可。

下面是示例代碼:

 public static Bitmap compressBitmapResource(Resources res, int resId, int inSampleSize) {
 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inJustDecodeBounds = false;
 options.inSampleSize = inSampleSize;
 return BitmapFactory.decodeResource(res, resId, options);
 }

2.2 圖片局部顯示

圖片壓縮會在一定程度上影響圖片質量和顯示效果,在某些場景下並不可取,例如地圖顯示時要求必須是高質量圖片,這時就不能進行壓縮處理,在這種場景下其實並不要求要一次顯示圖片的所有部分,可以考慮一次只加載和顯示圖片的特定部分,即***局部顯示***。

要實現局部顯示的效果,可以使用BitmapRegionDecoder 來實現,它就是用來對圖片的特定部分進行顯示的,尤其是在原圖片特別大而無法一次全部加載到內存的場景下,其聲明如下:

 /**
 * BitmapRegionDecoder can be used to decode a rectangle region from an image.
 * BitmapRegionDecoder is particularly useful when an original image is large and
 * you only need parts of the image.
 *
 * <p>To create a BitmapRegionDecoder, call newInstance(...).
 * Given a BitmapRegionDecoder, users can call decodeRegion() repeatedly
 * to get a decoded Bitmap of the specified region.
 *
 */
 public final class BitmapRegionDecoder { ... }

這裏也說明了如果使用BitmapRegionDecoder進行局部顯示:首先通過newInstance()創建實例,再利用decodeRegion()對指定區域的圖片內存創建Bitmap對象,進而在顯示控件中顯示。

過BitmapRegionDecoder.newInstance()創建解析器實例,其函數聲明如下:

 /**
 * Create a BitmapRegionDecoder from an input stream.
 * The stream's position will be where ever it was after the encoded data
 * was read.
 * Currently only the JPEG and PNG formats are supported.
 *
 * @param is The input stream that holds the raw data to be decoded into a
 *  BitmapRegionDecoder.
 * @param isShareable If this is true, then the BitmapRegionDecoder may keep a
 *   shallow reference to the input. If this is false,
 *   then the BitmapRegionDecoder will explicitly make a copy of the
 *   input data, and keep that. Even if sharing is allowed,
 *   the implementation may still decide to make a deep
 *   copy of the input data. If an image is progressively encoded,
 *   allowing sharing may degrade the decoding speed.
 * @return BitmapRegionDecoder, or null if the image data could not be decoded.
 * @throws IOException if the image format is not supported or can not be decoded.
 *
 * <p class="note">Prior to {@link android.os.Build.VERSION_CODES#KITKAT},
 * if {@link InputStream#markSupported is.markSupported()} returns true,
 * <code>is.mark(1024)</code> would be called. As of
 * {@link android.os.Build.VERSION_CODES#KITKAT}, this is no longer the case.</p>
 */
 public static BitmapRegionDecoder newInstance(InputStream is,
  boolean isShareable) throws IOException { ... }

需要注意的是,這只是BitmapRegionDecoder其中一個newInstance函數,除此之外還有其他的實現形式,讀者有興趣可以自己查閱。

在創建得到BitmapRegionDecoder實例後,可以調用decodeRegion方法來創建局部Bitmap對象,其函數聲明如下:

 /**
 * Decodes a rectangle region in the image specified by rect.
 *
 * @param rect The rectangle that specified the region to be decode.
 * @param options null-ok; Options that control downsampling.
 *  inPurgeable is not supported.
 * @return The decoded bitmap, or null if the image data could not be
 *  decoded.
 * @throws IllegalArgumentException if {@link BitmapFactory.Options#inPreferredConfig}
 *  is {@link android.graphics.Bitmap.Config#HARDWARE}
 *  and {@link BitmapFactory.Options#inMutable} is set, if the specified color space
 *  is not {@link ColorSpace.Model#RGB RGB}, or if the specified color space's transfer
 *  function is not an {@link ColorSpace.Rgb.TransferParameters ICC parametric curve}
 */
 public Bitmap decodeRegion(Rect rect, BitmapFactory.Options options) { ... }

由於這部分比較簡單,下面直接給出相關示例代碼:

 // 解析得到原圖的長寬值,方便後面進行局部顯示時指定需要顯示的區域。
 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inJustDecodeBounds = true;
 BitmapFactory.decodeResource(getResources(), R.drawable.test, options);
 int width = options.outWidth;
 int height = options.outHeight;

 try {
 // 創建局部解析器 
 InputStream inputStream = getResources().openRawResource(R.drawable.test);
 BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(inputStream,false);
 
 // 指定需要顯示的矩形區域,這裏要顯示的原圖的左上 1/4 區域。
 Rect rect = new Rect(0, 0, width / 2, height / 2);

 // 創建位圖配置,這裏使用 RGB_565,每個像素佔 2 字節。
 BitmapFactory.Options regionOptions = new BitmapFactory.Options();
 regionOptions.inPreferredConfig = Bitmap.Config.RGB_565;
 
 // 創建得到指定區域的 Bitmap 對象並進行顯示。
 Bitmap regionBitmap = decoder.decodeRegion(rect,regionOptions);
 ImageView imageView = (ImageView) findViewById(R.id.main_image);
 imageView.setImageBitmap(regionBitmap);
 } catch (Exception e) {
 e.printStackTrace();
 }

從測試結果看,確實只顯示了原圖的左上1/4區域的圖片內容,這裏不再貼出結果。

3. 加載多圖片

有時需要在應用中同時顯示多張圖片,例如使用ListView,GridView和ViewPager時,可能會需要在每一項都顯示一個圖片,這時情況就會變得複雜些,因爲可以通過滑動改變控件的可見項,如果每增加一個可見項就加載一個圖片,同時不可見項的圖片繼續在內存中,隨着不斷的增加,就會導致內存溢出。

爲了避免這種情況的內存溢出問題,就需要對不可見項對應的圖片資源進行回收,即當前項被滑出屏幕的顯示區域時考慮回收相關的圖片,這時回收策略對整個應用的性能有較大影響。

  • 立即回收:在當前項被滑出屏幕時立即回收圖片資源,但如果被滑出的項很快又被滑入屏幕,就需要重新加載圖片,這無疑會導致性能的下降。
  • 延遲迴收:在當前項被滑出屏幕時不立即回收,而是根據一定的延遲策略進行回收,這時對延遲策略有較高要求,如果延遲時間太短就退回到立即回收狀況,如果延遲時間較長就可能導致一段時間內,內存中存在大量的圖片,進而引發內存溢出。通過上面的分析,針對加載多圖的情況,必須要採取延遲迴收,而Android提供了一中基於LRU,即最近最少使用策略的內存緩存技術: LruCache, 其基本思想是,以強引用的方式保存外界對象,當緩存空間達到一定限制後,再把最近最少使用的對象釋放回收,保證使用的緩存空間始終在一個合理範圍內。

其聲明如下:

/**
 * A cache that holds strong references to a limited number of values. Each time
 * a value is accessed, it is moved to the head of a queue. When a value is
 * added to a full cache, the value at the end of that queue is evicted and may
 * become eligible for garbage collection.
 */
public class LruCache<K, V> { ... }

從聲明中,可以瞭解到其實現LRU的方式:內部維護一個有序隊列,每當其中的一個對象被訪問就被移動到隊首,這樣就保證了隊列中的對象是根據最近的使用時間從近到遠排列的,即隊首的對象是最近使用的,隊尾的對象是最久之前使用的。正是基於這個規則,如果緩存達到限制後,直接把隊尾對象釋放即可。

在實際使用中,爲了創建LruCache對象,首先要確定該緩存能夠使用的內存大小,這是效率的決定性因素。如果緩存內存太小,無法真正發揮緩存的效果,仍然需要頻繁的加載和回收資源;如果緩存內存太大,可能導致內存溢出的發生。在確定緩存大小的時候,要結合以下幾個因素:

  • 進程可以使用的內存情況
  • 資源的大小和需要一次在界面上顯示的資源數量
  • 資源的訪問頻率

下面給出一個簡單的示例:

 // 獲得進程可以使用的最大內存量
 int maxMemory = (int) Runtime.getRuntime().maxMemory();
 
 mCache = new LruCache<String, Bitmap>(maxMemory / 4) {
  @Override
  protected int sizeOf(String key, Bitmap value) {
   return value.getByteCount();
  }
 };

在示例中簡單地把緩存大小設定爲進程可以使用的內存的 1/4,當然在實際項目中,要考慮的因素會更多。需要注意的是,在創建LruCache對象的時候需要重寫sizeOf方法,它用來返回每個對象的大小,是用來決定當前緩存實際大小並判斷是否達到了內存限制。

在創建了LruCache對象後,如果需要使用資源,首先到緩存中去取,如果成功取到就直接使用,否則加載資源並放入緩存中,以方便下次使用。爲了加載資源的行爲不會影響應用性能,需要在子線程中去進行,可以利用AsyncTask來實現。

下面是示例代碼:

 public Bitmap get(String key) {
  Bitmap bitmap = mCache.get(key);
  if (bitmap != null) {
   return bitmap;
  } else {
   new BitmapAsyncTask().execute(key);
   return null;
  }
 }

 private class BitmapAsyncTask extends AsyncTask<String, Void, Bitmap> {
  @Override
  protected Bitmap doInBackground(String... url) {
   Bitmap bitmap = getBitmapFromUrl(url[0]);
   if (bitmap != null) {
    mCache.put(url[0],bitmap);
   }
   return bitmap;
  }

  private Bitmap getBitmapFromUrl(String url) {
   Bitmap bitmap = null;
   // 在這裏要利用給定的 url 信息從網絡獲取 bitmap 信息.
   return bitmap;
  }
 }

示例中,在無法從緩存中獲取資源的時候,會根據url信息加載網絡資源,當前並沒有給出完整的代碼,有興趣的同學可以自己去完善。

4. 總結

本文主要針對不同的圖片加載場景提出了不同的加載策略,以保證在加載和顯示過程中既然能滿足基本的顯示需求,又不會導致內存溢出,具體包括針對單個圖片的壓縮顯示,局部顯示和針對多圖的內存緩存技術,如若有表述不清甚至錯誤的地方,請及時提出,大家一起學習。

好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對神馬文庫的支持。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章