如何提高Android從文件中讀取圖像的效率

本篇文章給大家分享的是有關(guān)如何提高Android從文件中讀取圖像的效率,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比濱湖網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式濱湖網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋濱湖地區(qū)。費用合理售后完善,10年實體公司更值得信賴。

方法一

start_time = System.currentTimeMillis();
      
      BitmapFactory.Options options=new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      Bitmap bitmap=BitmapFactory.decodeFile(path,options);
      options.inSampleSize=calculateSize(options,width,height);
      options.inJustDecodeBounds=false;
      //整個圖像,下采樣
      bitmap=BitmapFactory.decodeFile(path,options);
      //部分圖像
      Bitmap patch=Bitmap.createBitmap(bitmap, 10, 10, 100, 100);
      
      end_time = System.currentTimeMillis();
      Log.v("BitmapTest", "UI time consume:"+(end_time - start_time));
      imageView.setImageBitmap(bitmap);
      patchView.setImageBitmap(patch);

操作很簡單,先將圖片文件的尺寸等信息讀取出來, 然后根據(jù)其尺寸計算其縮放比例,并將圖片中的一部分剪切出來。最后將圖片顯示在ImageView空間上。大致測了幾十次,得到的平均消耗時間為:72.75ms

方法二

啟動子線程

start_time = System.currentTimeMillis();
String path=Environment.getExternalStorageDirectory().getPath()+File.separator+"image1.jpg";
ImgThread imgThread=new ImgThread(msgHandler,path,width,height);
imgThread.start();

子線程中的操作,與1基本相同

BitmapFactory.Options options=new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap bitmap=BitmapFactory.decodeFile(path,options);
    options.inSampleSize=calculateSize(options,width,height);
    options.inJustDecodeBounds=false;
    //整個圖像,下采樣
    bitmap=BitmapFactory.decodeFile(path,options);
    //部分圖像
    Bitmap patch=Bitmap.createBitmap(bitmap, 10, 10, 100, 100);
    array=new ArrayList<Bitmap>(2);
    array.add(bitmap);
    array.add(patch);
    //Serializable傳遞
    Bundle bundle=new Bundle();    
    bundle.putSerializable("img", array);
    //Parcelable傳遞
    /*
    MyList l=new MyList(Parcel.obtain());
    l.array=array;
    bundle.putParcelable("img", l);
    */
    Message msg= new Message();
    msg.what=1;
    msg.setData(bundle);
    handler.sendMessage(msg);

將Bitmap傳回到UI線程并呈現(xiàn)

Bundle bundle=msg.getData();
          //Serializable傳遞
          ArrayList<Bitmap> array=(ArrayList<Bitmap>) bundle.getSerializable("img");
          //Parcelable傳遞
          //MyList l=(MyList)bundle.getParcelable("img");
          //ArrayList<Bitmap> array=l.array;//=(ArrayList<Bitmap>) bundle.getParcelable("img");
          Bitmap bitmap=array.get(0);
          Bitmap patch=array.get(1);
          end_time = System.currentTimeMillis();
          Log.v("BitmapTest", "Th time consume:"+(end_time - start_time));
          imageView.setImageBitmap(bitmap);
          patchView.setImageBitmap(patch);

方法二的平均消耗時間為:83.93ms

方法三

該方法需要新建一個類用來實現(xiàn)Parcelable接口

package com.example.bitmaptest;

import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;

public class MyList implements Parcelable{

  public ArrayList array;
  
  public MyList(Parcel in)
  {
    in.readValue(null);
  }
  
  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeValue(array);
  }

  public static final Parcelable.Creator<MyList> CREATOR = new Parcelable.Creator<MyList>() {
    @Override
    public MyList createFromParcel(Parcel source) {
      return new MyList(source);
    }
    @Override
    public MyList[] newArray(int size) {
      return new MyList[size];
    }
  };
}

在子線程中的操作

//Parcelable傳遞
    
    MyList l=new MyList(Parcel.obtain());
    l.array=array;
    bundle.putParcelable("img", l);

方法三的平均消耗時間為:87.35ms

結(jié)果分析

三種方法都是在魅族MX1型號的手機上測試的,理論上方法三應該比方法二快,但至少根據(jù)我的實驗結(jié)果來看,在傳送小數(shù)據(jù)量時(圖像大概是幾mB或幾百kB),數(shù)據(jù)的傳遞耗時并不是關(guān)鍵,兩種方法的耗時差不多。方法一由于沒有使用線程間的數(shù)據(jù)傳遞,因此耗時是最少的。

因此,我總結(jié)得到如下結(jié)論:

1、如果必須等到圖像加載完成才允許用戶操作的這種場景,可以直接在UI線程做圖像的操作,這時可以添加一個ProgressDialog用來提示正在加載。

2、如果需要一邊允許用戶操作一邊加載圖像的話,應該新開一個子線程,但是在數(shù)據(jù)量不大的情況下,Serializable和Parcelable差距不大。

3、總而言之,圖像的尺寸和數(shù)量不大時,在UI線程直接做圖像讀取等操作即可,但比較大時還是最好開個子線程。

以上就是如何提高Android從文件中讀取圖像的效率,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享文章:如何提高Android從文件中讀取圖像的效率
鏈接分享:http://www.muchs.cn/article32/ishipc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供電子商務、網(wǎng)站策劃用戶體驗、Google、企業(yè)網(wǎng)站制作全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設