欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳談Android從文件讀取圖像顯示的效率問題

 更新時(shí)間:2017年03月10日 08:55:59   投稿:jingxian  
下面小編就為大家?guī)硪黄斦凙ndroid從文件讀取圖像顯示的效率問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

因?yàn)閺奈募x取圖像到Bitmap是一件比較費(fèi)時(shí)的事情,所以研究了一下幾種可行的辦法,并做了對(duì)比。

首先解釋一下為什么耗時(shí),這是因?yàn)?,在從jpg或者png文件中讀取Bitmap時(shí),一來需要對(duì)外存進(jìn)行操作并且圖像文件一般都比較大,二來在創(chuàng)建Bitmap時(shí),基本都需要對(duì)原始圖像做操作,例如:降采樣、剪切、旋轉(zhuǎn)等等。所以如何高效的讀取圖片并呈現(xiàn)出來,是一個(gè)很值得研究的問題。

根據(jù)我的想法,大致想出了3種方案:

1、在當(dāng)前的UI線程直接讀取并操作圖像,然后呈現(xiàn)。

2、新開一個(gè)子線程讀取并操作圖像,然后利用Bundle中Serializable的相關(guān)方法將其傳回UI線程并呈現(xiàn)。

3、其他做法與2一樣,但是利用的是Bundle中Parcelable的相關(guān)方法。

方法一

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;
      //整個(gè)圖像,下采樣
      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);

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

方法二

啟動(dòng)子線程

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;
    //整個(gè)圖像,下采樣
    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);

方法二的平均消耗時(shí)間為:83.93ms

方法三

該方法需要新建一個(gè)類用來實(shí)現(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);
    

方法三的平均消耗時(shí)間為:87.35ms

結(jié)果分析

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

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

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

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

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

以上這篇詳談Android從文件讀取圖像顯示的效率問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論