Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 圖片處理記憶體優化系列:圖片牆案例實作

編輯:關於 Android 程式設計

本文要探討的核心,是圖片的記憶體快取機制,以及如何從網路非同步抓取圖片並呈現在圖片牆上。

工程效果圖如下:

vcq9o6zU2rG+tdjDu9PQu7q05qOs0rLDu9PQttTNvMasvfjQ0NG5y/W7r6OouvPG2tTZ1/bV4tCpstnX96OpoaM8L3A+DQo8cD7K18/Iv7S0+sLro6xNYWluQWN0aXZpdHm1xLK8vtbOxLz+IGFjdGl2aXR5X21haW4ueG1sIMjnz8I8L3A+DQo8cHJlIGNsYXNzPQ=="brush:java;">

以下是程式中幾個比較不常見的屬性說明(其實查一下官方文件也能找到):

 android:numColumns="auto_fit" 意思是圖片牆分成幾列 :auto_fit自適應 android:stretchMode="columnWidth" 圖片牆右邊多出來或者少的區域適配方式:在列寬上進行加減來適配

接著來看為 GridView 準備的 adapter 所使用的單項 View 布局檔案 view_photo.xml:

    

布局檔案相當簡單,接下來要解決的是圖片來源問題。這裡我使用本地的 tomcat 伺服器,把圖片放在本機的 tomcat 上,自行定義好連結來進行存取:

tomcat 上的檔案如下圖:

圖片連結類如下:

 package com.jishihuitong.photowalldemo; /** * Created by hss on 2016/7/27. */ public class Images { public static final String[] IMAGE = new String[]{ "https://10.0.2.2:8080/images/01.png", "https://10.0.2.2:8080/images/02.png", "https://10.0.2.2:8080/images/03.png", "https://10.0.2.2:8080/images/04.png", "https://10.0.2.2:8080/images/05.png", "https://10.0.2.2:8080/images/06.png", "https://10.0.2.2:8080/images/07.png", "https://10.0.2.2:8080/images/08.png", "https://10.0.2.2:8080/images/09.png", "https://10.0.2.2:8080/images/10.png", "https://10.0.2.2:8080/images/11.png", "https://10.0.2.2:8080/images/12.png", "https://10.0.2.2:8080/images/13.png", "https://10.0.2.2:8080/images/14.png", "https://10.0.2.2:8080/images/15.png", "https://10.0.2.2:8080/images/16.png", "https://10.0.2.2:8080/images/17.png", "https://10.0.2.2:8080/images/18.png", "https://10.0.2.2:8080/images/19.png", "https://10.0.2.2:8080/images/20.png", "https://10.0.2.2:8080/images/21.png", "https://10.0.2.2:8080/images/22.png", "https://10.0.2.2:8080/images/23.png", "https://10.0.2.2:8080/images/24.png", "https://10.0.2.2:8080/images/25.png", "https://10.0.2.2:8080/images/26.png", "https://10.0.2.2:8080/images/27.png", "https://10.0.2.2:8080/images/28.png", "https://10.0.2.2:8080/images/29.png", "https://10.0.2.2:8080/images/30.png", "https://10.0.2.2:8080/images/31.png", "https://10.0.2.2:8080/images/32.png", "https://10.0.2.2:8080/images/33.png", "https://10.0.2.2:8080/images/34.png", "https://10.0.2.2:8080/images/35.png", "https://10.0.2.2:8080/images/36.png", "https://10.0.2.2:8080/images/37.png", "https://10.0.2.2:8080/images/38.png", "https://10.0.2.2:8080/images/39.png", "https://10.0.2.2:8080/images/40.png", "https://10.0.2.2:8080/images/41.png", "https://10.0.2.2:8080/images/42.png", "https://10.0.2.2:8080/images/43.png", "https://10.0.2.2:8080/images/44.png", "https://10.0.2.2:8080/images/45.png", "https://10.0.2.2:8080/images/46.png", "https://10.0.2.2:8080/images/47.png", "https://10.0.2.2:8080/images/48.png", "https://10.0.2.2:8080/images/49.png", "https://10.0.2.2:8080/images/50.png", "https://10.0.2.2:8080/images/51.png", "https://10.0.2.2:8080/images/52.png", "https://10.0.2.2:8080/images/53.png", "https://10.0.2.2:8080/images/54.png", "https://10.0.2.2:8080/images/55.png", "https://10.0.2.2:8080/images/56.png", "https://10.0.2.2:8080/images/67.png", "https://10.0.2.2:8080/images/58.png", "https://10.0.2.2:8080/images/59.png", "https://10.0.2.2:8080/images/60.png", "https://10.0.2.2:8080/images/61.png", "https://10.0.2.2:8080/images/62.png", "https://10.0.2.2:8080/images/63.png", "https://10.0.2.2:8080/images/64.png", "https://10.0.2.2:8080/images/65.png", "https://10.0.2.2:8080/images/66.png", "https://10.0.2.2:8080/images/67.png", "https://10.0.2.2:8080/images/68.png", "https://10.0.2.2:8080/images/69.png", "https://10.0.2.2:8080/images/70.png", "https://10.0.2.2:8080/images/71.png", "https://10.0.2.2:8080/images/72.png", "https://10.0.2.2:8080/images/73.png", "https://10.0.2.2:8080/images/74.png", "https://10.0.2.2:8080/images/75.png", "https://10.0.2.2:8080/images/76.png", "https://10.0.2.2:8080/images/77.png", "https://10.0.2.2:8080/images/78.png" }; } 

接著來看 MainActivity 裡的程式碼:

 package com.jishihuitong.photowalldemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.GridView; public class MainActivity extends AppCompatActivity { /** * 圖片牆GridView的適配器Adapter */ private PhotoWallAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView photowall = (GridView) findViewById(R.id.gv_photowall); adapter = new PhotoWallAdapter(getApplicationContext(),0, Images.IMAGE,photowall); photowall.setAdapter(adapter); } @Override protected void onDestroy() { super.onDestroy(); //activity退出的時候取消所有正在下載的圖片任務 adapter.cancelAllTasks(); } } 

整體核心思路是讓 PhotoWallAdapter 繼承 ArrayAdapter 並覆寫其中的 getView 方法,把 View 載入進來:

1、在構造方法中把 GridView 物件傳進來,方便後續引用
2、首先在 Adapter 的構造方法裡實例化 LruCache 物件,
3、實例化任務集合,BitmapWorkerTask 的參數型別是 String,回傳值型別是 Bitmap
4、為 GridView 設定滾動監聽:第一次進入 App 時,把畫面上出現的所有圖片都載入出來;並非第一次進入時,則判斷當前是滾動狀態還是停止狀態,只有在停止狀態時才去載入圖片,滾動狀態下則取消所有下載任務
5、圖片下載完成後,把它快取到記憶體中

具體過程請見以下程式碼:

MainActivity 裡面沒有多少程式碼,最主要的重點在這個 PhotoWallAdapter:

 package com.jishihuitong.photowalldemo; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.LruCache; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.ArrayAdapter; import android.widget.GridView; import android.widget.ImageView; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashSet; /** * Created by hss on 2016/7/27. */ public class PhotoWallAdapter extends ArrayAdapter { /** * GridView實例 */ private GridView mGridView; /** * 圖片內存緩存類 */ private LruCache mLruCache; /** * 任務管理集合 */ private HashSet mTaskCollection; private View view; /** * 第一個可以看見的條目下標 */ private int mFirstVisibleItem; /** * 可以看見的條目總數 */ private int mVisibleItemCount; /** * 是否是第一次進入 */ private boolean isFirstEnter = true; /** * 全局tag標記 */ private static final String TAG = "PhotoWallAdapter"; public PhotoWallAdapter(Context context, int resource, String[] objects, GridView gridView) { super(context, resource, objects); this.mGridView = gridView; //獲取到當前系統的最大內存 int maxMemory = (int) Runtime.getRuntime().maxMemory(); //我們使用最大內存的1/8作為本應用緩存圖片的空間大小 int cacheMemory = maxMemory / 8; //創建內存緩存對象,參數為定義好的緩存大小, mLruCache = new LruCache(cacheMemory) { //重寫sizeOf()方法,默認返回圖片的數量 @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getByteCount(); } }; //實例化任務集合 mTaskCollection = new HashSet(); //給gridView設置滾動監聽 mGridView.setOnScrollListener(new AbsListView.OnScrollListener() { //開始滾動的時候,取消下載任務, @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //成員變量隨著滾動而變化 mFirstVisibleItem = firstVisibleItem; mVisibleItemCount = visibleItemCount; //如果第一次進來的時候,滑動狀態下可以加載圖片, if(isFirstEnter && visibleItemCount>0){ //加載可見范圍內全部的圖片 loadBitmaps(firstVisibleItem,visibleItemCount); isFirstEnter = false; } } //處於不滾動狀態的時候,再開始任務 @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if(scrollState== AbsListView.OnScrollListener.SCROLL_STATE_IDLE){ LogUtil.d(TAG,"mFirstVisibleItem = "+mFirstVisibleItem+" mVisibleItemCount = "+mVisibleItemCount); loadBitmaps(mFirstVisibleItem, mVisibleItemCount); }else{ //取消所有的下載圖片的任務 cancelAllTasks(); } } }); } /** * 取消所有下載圖片的任務 */ public void cancelAllTasks() { if(mTaskCollection!=null){ for (BitmapWorkerTask task: mTaskCollection) { task.cancel(false); } } } /** * 定義任務,執行任務,選擇方法從本地和網絡上加載圖片 * @param firstVisibleItem gridView中可以看見的第一個角標 * @param visibleItemCount gridView中可以看見的所有圖片的總數 */ private void loadBitmaps(int firstVisibleItem, int visibleItemCount) { try{ for (int i = firstVisibleItem; i  { private String imageUrl; //需要在後台進程中干的事情 @Override protected Bitmap doInBackground(String... params) { //從參數中獲取到傳進來的圖片URL imageUrl = params[0]; //從網絡上下載圖片 Bitmap bitmap = downLoadBitmap(params[0]); //如果下載到的圖片不為空,把圖片保存進內存中 if(bitmap!=null){ addBitmapToCacheMemory(params[0],bitmap); } return bitmap; } //該方法在上面的方法,DoInBackground之後運行,方法裡面的參數就是上面的方法的返回值 @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); ImageView photo = (ImageView) mGridView.findViewWithTag(imageUrl); if(bitmap!=null&&photo!=null){ photo.setImageBitmap(bitmap); } //完了記得把任務清除掉 mTaskCollection.remove(this); } /** * 從網絡上下載圖片,在後台進程中運行 * @param imageUrl * @return */ private Bitmap downLoadBitmap(String imageUrl) { Bitmap bitmap = null; HttpURLConnection conn = null; try{ URL url = new URL(imageUrl); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5*1000); conn.setReadTimeout(10 * 1000); conn.setDoInput(true); conn.setDoOutput(true); bitmap = BitmapFactory.decodeStream(conn.getInputStream()); }catch(Exception e){ e.printStackTrace(); }finally { if(conn!=null){ conn.disconnect(); } } return bitmap; } } } 

執行時別忘了在清單檔案(AndroidManifest.xml)中加上連網權限。

下載源碼

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved