自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

處理來自UI線程的位圖

移動開發(fā) Android
本文通過具體方法講解了如何處理來自UI線程的位圖,希望讀完本文對各位讀者朋友有一定的幫助和啟發(fā)。

BitmapFactory的decode()方法,在Load Large Bitmaps Efficiently要 點中進行討論,不應該執(zhí)行在主UI線程如果要讀取源數(shù)據(jù)從磁盤或網(wǎng)絡位置(或相對內(nèi)存來說任何別的真實來源).該數(shù)據(jù)需要加載的時間是不可預知的,并取決 于多種因素(從磁盤或網(wǎng)絡的讀取速度,圖像大小,CPU的功率,等).如果這些任務阻塞UI線程,系統(tǒng)標志您的應用程序無響應,用戶可以選擇關(guān)閉它響應 (有關(guān)更多信息,請參閱Designing for Responsiveness).

本文將引導您通過在后臺線程中使用AsyncTask處理位圖,并告訴您如何處理并發(fā)問題.

使用一個異步任務

AsyncTask類提供了一種簡單的方式來在一個后臺線程中執(zhí)行許多任務,并且把結(jié)果反饋給UI線程.使用的方法是,創(chuàng)建一個繼承與它的子類并且實現(xiàn)提供的方法.這里是一個使用AsyncTask和decodeSampledBitmapFromResource()加載一個大圖片到ImageView中的例子:

  1. class BitmapWorkerTask extends AsyncTask { 
  2.     private final WeakReference imageViewReference; 
  3.     private int data = 0
  4.  
  5.     public BitmapWorkerTask(ImageView imageView) { 
  6.         // Use a WeakReference to ensure the ImageView can be garbage collected 
  7.         imageViewReference = new WeakReference(imageView); 
  8.     } 
  9.  
  10.     // Decode image in background. 
  11.     @Override 
  12.     protected Bitmap doInBackground(Integer... params) { 
  13.         data = params[0]; 
  14.         return decodeSampledBitmapFromResource(getResources(), data, 100100)); 
  15.     } 
  16.  
  17.     // Once complete, see if ImageView is still around and set bitmap. 
  18.     @Override 
  19.     protected void onPostExecute(Bitmap bitmap) { 
  20.         if (imageViewReference null) { 
  21.             final ImageView imageView = imageViewReference.get(); 
  22.             if (imageView != null) { 
  23.                 imageView.setImageBitmap(bitmap); 
  24.             } 
  25.         } 
  26.     } 
  27.  } 

對于ImageView來說WeakReference確保那時AsyncTask并不會阻礙ImageView和任何它的引用被垃圾回收期回收.不能保證ImageView在任務完成后仍然存在,所以你必須在onPostExecute()方法中檢查它的引用.ImageView可能不再存在,如果例如,如果在任務完成之前用戶退出了活動或者配置發(fā)生了變化.

為了異步地加載位圖,簡單地創(chuàng)建一個新的任務并且執(zhí)行它:

  1. public void loadBitmap(int resId, ImageView imageView) { 
  2.     BitmapWorkerTask task = new BitmapWorkerTask(imageView); 
  3.     task.execute(resId); 
  4.  } 

處理并發(fā)

常見的視圖組件例如ListView和GridView如在上一節(jié)中當和AsyncTask結(jié)合使用時引出了另外一個問題.為了優(yōu)化內(nèi)存,當用戶滾 動時這些組件回收了子視圖.如果每個子視圖觸發(fā)一個AsyncTask,當它完成時沒法保證,相關(guān)的視圖還沒有被回收時已經(jīng)用在了別的子視圖當中.此外, 還有異步任務開始的順序是不能保證他們完成的順序.

這篇文章透過Multithreading for Performance功能討論處理并發(fā),并且提供了一個當任務完成后ImageView將一個引用存儲到后面能被檢查的AsyncTask的解決方案. 使用類似的方法,從上一節(jié)的AsyncTask可以擴展到遵循類似的模式.

創(chuàng)建一個專用的Drawable的子類來存儲一個引用備份到工作任務中.在這種情況下,一個BitmapDrawable被使用以便任務完成后一個占位符圖像可以顯示在ImageView中:

  1. static class AsyncDrawable extends BitmapDrawable { 
  2.     private final WeakReference bitmapWorkerTaskReference; 
  3.  
  4.     public AsyncDrawable(Resources res, Bitmap bitmap, 
  5.             BitmapWorkerTask bitmapWorkerTask) { 
  6.         super(res, bitmap); 
  7.         bitmapWorkerTaskReference = 
  8.             new WeakReference(bitmapWorkerTask); 
  9.     } 
  10.  
  11.     public BitmapWorkerTask getBitmapWorkerTask() { 
  12.         return bitmapWorkerTaskReference.get(); 
  13.     } 
  14.  } 

執(zhí)行BitmapWorkerTask前,你創(chuàng)建一個AsyncDrawable,并將其綁定到目標ImageView:

  1. public void loadBitmap(int resId, ImageView imageView) { 
  2.     if (cancelPotentialWork(resId, imageView)) { 
  3.         final BitmapWorkerTask task = new BitmapWorkerTask(imageView); 
  4.         final AsyncDrawable asyncDrawable = 
  5.                 new AsyncDrawable(getResources(), mPlaceHolderBitmap, task); 
  6.         imageView.setImageDrawable(asyncDrawable); 
  7.         task.execute(resId); 
  8.     } 
  9.  } 

如果別的正在運行的任務已經(jīng)和這個ImageView關(guān)聯(lián),cancelPotentialWork引用在上面的代碼示例檢查中.如果這樣,它試圖通過調(diào)用cancel()取消先前的任務.在少數(shù)情況下,新的任務數(shù)據(jù)匹配現(xiàn)有的任務,而且并不需要做什么.下面是實現(xiàn) cancelPotentialWork:

  1. public static boolean cancelPotentialWork(int data, ImageView imageView) { 
  2.     final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); 
  3.     if (bitmapWorkerTask != null) { 
  4.         final int bitmapData = bitmapWorkerTask.data; 
  5.         if (bitmapData != data) { 
  6.             // Cancel previous task 
  7.             bitmapWorkerTask.cancel(true); 
  8.         } else { 
  9.             // The same work is already in progress 
  10.             return false
  11.         } 
  12.     } 
  13.     // No task associated with the ImageView, or an existing task was cancelled 
  14.     return true
  15.  } 

一個幫助方法,getBitmapWorkerTask(),使用以上來檢索一個和特定ImageView相關(guān)的任務:

  1. private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) { 
  2.    if (imageView != null) { 
  3.        final Drawable drawable = imageView.getDrawable(); 
  4.        if (drawable instanceof AsyncDrawable) { 
  5.            final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable; 
  6.            return asyncDrawable.getBitmapWorkerTask(); 
  7.        } 
  8.     } 
  9.     return null
  10.  } 

這***一步是在BitmapWorkerTask更新onPostExecute()方法,以便任務取消時并且當前任務和這個ImageView關(guān)聯(lián)時進行檢查:

  1. class BitmapWorkerTask extends AsyncTask { 
  2.     ... 
  3.     @Override 
  4.     protected void onPostExecute(Bitmap bitmap) { 
  5.         if (isCancelled()) { 
  6.             bitmap = null
  7.         } 
  8.         if (imageViewReference != null && bitmap != null ) { 
  9.             final ImageView imageView = imageViewReference.get(); 
  10.             final BitmapWorkerTask bitmapWorkerTask = 
  11.                     getBitmapWorkerTask(imageView); 
  12.             if (this == bitmapWorkerTask && imageView != null) { 
  13.                 imageView.setImageBitmap(bitmap); 
  14.             } 
  15.         } 
  16.     } 
  17.  } 

現(xiàn)在這個實現(xiàn)適合使用ListView和GridView控 件組件以及回收其子視圖的任何其他組件.在你正常地給你的ImageView控件設置圖片時簡單地調(diào)用loadBitmap就行了.例如,在一個 GridView中實現(xiàn)的方式是在支持的適配中的[android.view.View, android.view.ViewGroup) getView()](http://docs.eoeandroid.com/reference/android/widget /Adapter.html#getView(int,)方法中.

責任編輯:閆佳明 來源: my.eoe.cn
相關(guān)推薦

2014-04-08 14:19:06

Android開發(fā)UI線程

2009-03-19 15:52:50

Silverlight位圖WPF

2025-02-10 07:05:00

WinFormUI線程

2009-04-10 22:28:29

企業(yè)安全信息安全RSA

2015-11-18 18:56:36

Java多線程處理

2024-10-24 17:13:55

WinformUI多線程

2017-03-13 10:41:33

iOSUI操作主線程

2009-11-27 08:50:51

Chrome OSWindows

2021-08-27 22:07:55

Oracle索引位圖

2021-01-11 16:29:08

加密貨幣金融銀行

2009-07-17 10:37:05

C#多線程

2015-08-07 10:40:31

UI主線程

2009-07-15 18:06:38

Swing線程

2010-04-14 09:20:26

.NET多線程

2024-04-30 12:56:00

多線程.NET

2009-04-10 14:38:17

Oracle高手位圖索引

2010-04-08 10:57:04

Oracle編程

2011-11-22 08:52:49

2024-06-18 08:22:51

ActivityUI線程

2022-07-21 09:00:44

CIO數(shù)字時代數(shù)字化
點贊
收藏

51CTO技術(shù)棧公眾號