Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 生成縮略圖的三種方法與效能比較

編輯:關於 Android 程式設計

在 Android 中對大圖片進行縮放,體驗一直算不上理想。本文整理了三種常見的縮圖做法,並附上各自的效能表現,供大家參考比較。

方法一:BitmapFactory 搭配 BitmapFactory.Options

這是最推薦的做法。BitmapFactory.Options 中有幾個關鍵欄位非常實用:

inJustDecodeBounds:設為 true 時,解碼器只讀取圖片資訊而不會把 Bitmap 載入記憶體(回傳 null),效率極高,同時你仍能取得圖片的寬高。

outHeight / outWidth:無論 inJustDecodeBounds 為何狀態,都能取得圖片的最終高度與寬度。

inSampleSize:設為大於 1 的值時,會要求解碼器對原圖進行抽樣,回傳縮小的圖片以節省記憶體,這正是縮放比的來源。實務上有個小技巧:

inSampleSize =(outHeight/Height + outWidth/Width)/ 2

實測證明,用這個公式縮放出來的圖片品質依舊很不錯。最後再用 BitmapFactory.decodeFile(path, options) 生成即可。由於 Bitmap 只載入記憶體一次,解析速度很快,效率也高。

方法二:Bitmap 搭配 Matrix 縮放

先取得原始 Bitmap,再在其基礎上生成新圖片。這種做法效率相對較低,因為原圖已經完整載入記憶體。

方法三:ThumbnailUtils(2.2 新增)

從 API 來看,這個類只有三個靜態方法:createVideoThumbnail、extractThumbnail(Bitmap source, int width, int height, int options) 與 extractThumbnail(Bitmap source, int width, int height)。它其實是對前面 BitmapFactory.Options 與 Matrix 的一層封裝,效率略優於方法二。

以下是完整的實作範例:

 public class ResolvePicture extends Activity{ private static String tag="ResolvePicture"; Drawable bmImg; ImageView imView; ImageView imView2; TextView text; String theTime; long start,stop; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); text=(TextView)findViewById(R.id.text); imView=(ImageView)findViewById(R.id.imageShow); imView2=(ImageView)findViewById(R.id.image2); Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.pic); start=System.currentTimeMillis(); imView2.setImageDrawable(resizeImage2("/sdcard/2.jpeg",200,100)); stop=System.currentTimeMillis(); String theTime=String.format("\n1iterative:(%dmsec)", stop-start); start=System.currentTimeMillis(); imView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,200,100)); stop=System.currentTimeMillis(); theTime+=String.format("\n2iterative:(%dmsec)", stop-start); text.setText(theTime); } //使用Bitmap加Matrix來縮放 public static Drawable resizeImage(Bitmap bitmap, int w, int h) { Bitmap BitmapOrg=bitmap; int width=BitmapOrg.getWidth(); int height=BitmapOrg.getHeight(); int newWidth=w; int newHeight=h; float scaleWidth=((float)newWidth)/width; float scaleHeight=((float)newHeight)/height; Matrix matrix=new Matrix(); matrix.postScale(scaleWidth,scaleHeight); Bitmap resizedBitmap=Bitmap.createBitmap(BitmapOrg,0,0,width, height,matrix,true); return new BitmapDrawable(resizedBitmap); } //使用BitmapFactory.Options的inSampleSize參數來縮放 public static Drawable resizeImage2(String path, int width, int height) { BitmapFactory.Options options=new BitmapFactory.Options(); options.inJustDecodeBounds=true; BitmapFactory.decodeFile(path,options); int outWidth=options.outWidth; int outHeight=options.outHeight; options.inDither=false; options.inPreferredConfig=Bitmap.Config.ARGB_8888; options.inSampleSize=1; if(outWidth!=0&&outHeight!=0&&width!=0&&height!=0) { int sampleSize=(outWidth/width+outHeight/height)/2; Log.d(tag,"sampleSize="+sampleSize); options.inSampleSize=sampleSize; } options.inJustDecodeBounds=false; return new BitmapDrawable(BitmapFactory.decodeFile(path,options)); } //圖片保存 private void saveThePicture(Bitmap bitmap) { File file=new File("/sdcard/2.jpeg"); try { FileOutputStream fos=new FileOutputStream(file); if(bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos)) { fos.flush(); fos.close(); } } catch(FileNotFoundException e1) { e1.printStackTrace(); } catch(IOException e2) { e2.printStackTrace(); } } }

綜合來看,若要兼顧效率與記憶體,方法一(BitmapFactory.Options 的 inSampleSize)是最值得優先採用的方案;ThumbnailUtils 則勝在簡單易用,適合快速整合的場合;而 Bitmap + Matrix 的做法雖然直觀,卻因為需要先把原圖完整載入而顯得笨重。實際開發中,建議根據圖片大小與場景需求靈活取捨。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved