Android 大位元影像載入

來源:互聯網
上載者:User

標籤:

說明:沒對圖片進行緩衝處理,只是使用軟引用進行位元影像資源的釋放,從而避免記憶體流失。

對位元影像進行解碼顯示:

 1 public Bitmap decodeBitmap(Resources resources, int resId, int reqWith, reqHeight ) { 2     //對位元影像進行解碼的參數設定 3     BitmapFactory.Options options = new BitmapFactory.Options(); 4     //在對位元影像進行解碼的過程中,避免申請記憶體空間 5     options.inJustDecodeBounds = true; 6     BimapFactory.decodeResource(resources, resId, options); 7     //對圖片進行一定比例的壓縮處理 8     options.inSampleSize = caculateInSimpleSize(options, reqWith, reqHeight); 9     //真正輸出位元影像10     options.inJustDecodeBounds = false;11     return BimapFactory.decodeResource(resources, resId, options);12 }

計算圖片壓縮比例:

public int caculateInSimpleSize(BitmapFactory.Options options, int reqWidth, int reqHeight){    //    int imageHeight = options.outHeight;    int imageWidth = options.outWidth;    int inSimpeSize = 1; // 壓縮比例    if (imageHeight > reqHeight || imageWidth > reqWidth)  {          final int heightRatio = Math.round((float) imageHeight  / (float) reqHeight );          final int widthRatio = Math.round((float) imageWidth / (float) reqWidth);          inSimpleSize = heightRatio < widthRatio ? heightRatio : widthRatio ;    }    return inSimpleSize;}

網狀圖片請求:

 1 public static byte[] sendPost (String path){ 2      HttpClient httpClient = new DefaultHttpClient(); 3      HttpPost httpPost = new HttPost (path); 4      HttpResponse response = null; 5      try { 6           response = httpClient.execute(httpPost); 7           if (response.getStatusLine().getStatusCode() == 200) { 8                return EntityUtils.toByteArray(response.getEntity()); 9           }10      }  catch (Exception e) {11            e.printStackTrace();12      }  finally {13            httpClient.getConnectionManager().shutdown();14      }15      return null;16 }

批量載入大位元影像:

 1 //在adpter中的getView中處理顯示 2 public View getView(int position, View converView, ViewGroup parent) { 3       View view = null; 4       if (converView == null ){ 5             view = LayoutInflater.from(MainActivity.this,).inflate(R.layout.item, null); 6       } else { 7             view = converView; 8       } 9       ImageView imageView = (ImageView) view.findViewById(R.id.item_image);10       //擷取圖片11       loadBitmap(path[position], imageView);12       return view;13 }
//在滑動ListView時,會對舊的布局進行資源回收,如果ListView結合非同步任務操作時,不能確保重用的布局被及時回收。static class AsyncDrawable extends BitmapDrawable{     private final SoftReference<BitmapWorkerTask> softReference;     public AsyncDrawable (Resources resources, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {           super(resources, bitmap);           softReference = SoftReference<MainActivity.BitmapWorkerTask>(bitmapWorkerTask);    }         public BitmapWorkerTask getBitmapWorkerTask() {           return softReference.get();    }}/**非同步任務**/class BitmapWorkerTask extends AsyncTask<String, void, Bitmap> {     private SoftReference<ImageView> imageSoftReference;     private String data = "";     public BitmapWorkerTask (ImageView imageView) {           imageSoftReference = new SoftReference<ImageView>(imageView);     }     @Override     protected Bitmap doInBackground (String... params) {          data = params[0];          byte[] result = sendPost(data);          // 對位元影像解碼, 返回100*100          return decodeBitmap(result, 100, 100);     }     @Override     potected void onPostExecute(Bitmap bitmap) {           super.onPostExecute(bitmap);           if (isCancelled()) {                bitmap = null;           }           if (imageSoftReference != null && bitmap  != null) {                final ImageView imageView = imageSoftReference.get();                final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);                // 載入非同步任務是獨立的線程,保證引用imageView和非同步任務有關聯才可以。                if (this == bitmapWorkerTask && imageView != null) {                       imageView.setImageBitmap(bitmap);                }           }      }}private static BitmapWorkerTask getBitmapWorkerTask (ImageView imageView) {              if (imageView != null) {             final Drawable drawable = imageView.getDrawable();             if (drawable instanceof AsyncDrawable) {                   final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;                   return asyncDrawable.getBitmapWorkerTask();             }       }       return null;}// 檢查是否有另外一個執行的非同步任務和imageView來綁定,前一個非同步任務進行取消操作public static boolean cancelPotntialWork(String data, ImageView imageView) {       final BitmapWorkerTask  bitmapWorkerTask = getBitmapWorkerTask(imageView);       if (bitmapWorkerTask  != null) {               final String bitmapData = bitmapWorkerTask.data;               if (bitmapData != data) {                     bitmapWorkerTask.cancel(true);               } else {                     return false;               }       }       return true;}//載入圖片public void loadBitmap(String data, ImageView imageView) {       Bitmap placeBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.empty_photo);       if (cancelPotntialWork(data, imageView)) {             final BitmapWorkerTask task = new BitmapWorkerTask(imageView);             final AsyncDrawable asyncDrawable = new AsyncDrawable(getResources(), placeBitmap, task);             imageView.setImageDrawable(asyncDrawable);             task.execute(data);       }}

 

Android 大位元影像載入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.