在Android Studio中利用List Fragments創建相冊Gallery
摘要:本文講解了在 Android Studio 中如何通過 List Fragments 創建一個以縮略圖整列展示設備中圖片的相冊應用。
這是關于在Android Studio中通過 fragments 進行相機方面開發的五篇系列文章的第二篇。如果你還沒有把我放在 GitHub 上的范例程序克隆下來,那么請先去這里獲取最新代碼。本文主要包含的是 “SimplePhotoGalleryListFragment” 這個 Fragment。
注意:本范例中所涉及的 List Fragment 的用法,可以在 list fragments 這篇文章中找到詳細的講解。
AsyncTaskLoaders 以及 Fragments
加載整個圖片庫到List是一個運算量比較密集,強度很高任務。因此,我們希望利用 Android 提供的AsyncTaskLoader 通過異步加載解決這個問題。在這里,我已經寫好了一個自定義AsyncTaskLoader工具類用加載圖庫中圖片的。我把它命名為:PhotoGalleryImageProvider,可以在源碼中找到。
Fragments 提供了一種特殊的接口給異步任務的 Loader 以便于自動觸發異步加載任務。我們的圖庫列表在Fragment中看起來如下面的代碼:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);// Create an empty loader and pre-initialize the photo list items as an empty list. Context context = getActivity().getBaseContext(); // Set up empty mAdapter mPhotoListItem = new ArrayList() ; mAdapter = new PhotoAdapter(context, R.layout.photo_item, mPhotoListItem, false); // Prepare the loader. Either re-connect with an existing one, // or start a new one. getLoaderManager().initLoader(0, null, this); }</pre> <p>請注意最后的這一行:</p>
getLoaderManager().initLoader(0, null, this);這一行的作用就是自動啟用 AsyncLoader。AsyncLoader的相關代碼放在這個Class文件的后面。
/*** Loader Handlers for loading the photos in the background. */ @Override public Loader<List> onCreateLoader(int id, Bundle args) { // This is called when a new Loader needs to be created. This // sample only has one Loader with no arguments, so it is simple. return new PhotoGalleryAsyncLoader(getActivity()); }</pre> <p>每次后臺任務成功獲取到圖庫中的圖片時,則會回調下面這個函數:</p>
@Override public void onLoadFinished(Loader<List> loader, List data) { // Set the new data in the mAdapter. mPhotoListItem.clear();for(int i = 0; i < data.size();i++){ PhotoItem item = data.get(i); mPhotoListItem.add(item); } mAdapter.notifyDataSetChanged(); resolveEmptyText(); cancelProgressDialog(); }</pre> <p>其中,PhotoItem(用作給 Adapter 存儲數據)的數組包含了指向所有圖庫中圖片的縮略圖以及全尺寸圖片的URL。一旦獲取這些數據,Adapter必定會通過 “notifyDataSetChanged” 回調來通知出去,從而刷新當前的圖片列表。</p>
通過游標(cursor)來獲取縮略圖
之前我提到過,我已經提供了一個工具類可以用游標方便的去獲取圖庫中圖片的縮略圖,這個工具類叫做“PhotoGalleryImageProvider”。這個類的主要用法如下:
/*** Fetch both full sized images and thumbnails via a single query. * Returns all images not in the Camera Roll. * @param context * @return */ public static List getAlbumThumbnails(Context context){ final String[] projection = {MediaStore.Images.Thumbnails.DATA,MediaStore.Images.Thumbnails.IMAGE_ID}; Cursor thumbnailsCursor = context.getContentResolver().query( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, // Which columns to return null, // Return all rows null, null); ... return result; }</pre> <p>直接用Android 提供的<a href="/misc/goto?guid=4959555142992975982">Cursors</a> 去后臺獲取圖片是比較簡單的一種用法。另一種高級的用法則是使用 <a href="/misc/goto?guid=4959555143082063271">CursorLoader</a> 來操作Cursor。CursorLoader 內建了一個 AsyncTaskLoader 可以用來自動處理后臺的加載進程。由于需要同時渲染獲取到圖片的縮略圖和完整尺寸的圖片,所以盡管使用帶有CursorLoader的AsyncTask也可以得到同樣的結果,但我還是選擇了寫一個自定義的Task Loader。</p>
除了這些必須了解的內容,你現在可以獲取范例代碼然后好好享受學習的旅程了!
相關連接
- List all camera images (用作分部創建cursor loader類) </ul>
原文鏈接: airpair 翻譯: 伯樂在線 - zerob13
譯文鏈接: http://blog.jobbole.com/73138/
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!