編輯:關於 Android 程式設計
在 Android 中對大圖片進行縮放,體驗一直算不上理想。本文整理了三種常見的縮圖做法,並附上各自的效能表現,供大家參考比較。
這是最推薦的做法。BitmapFactory.Options 中有幾個關鍵欄位非常實用:
inJustDecodeBounds:設為 true 時,解碼器只讀取圖片資訊而不會把 Bitmap 載入記憶體(回傳 null),效率極高,同時你仍能取得圖片的寬高。
outHeight / outWidth:無論 inJustDecodeBounds 為何狀態,都能取得圖片的最終高度與寬度。
inSampleSize:設為大於 1 的值時,會要求解碼器對原圖進行抽樣,回傳縮小的圖片以節省記憶體,這正是縮放比的來源。實務上有個小技巧:
inSampleSize =(outHeight/Height + outWidth/Width)/ 2
實測證明,用這個公式縮放出來的圖片品質依舊很不錯。最後再用 BitmapFactory.decodeFile(path, options) 生成即可。由於 Bitmap 只載入記憶體一次,解析速度很快,效率也高。
先取得原始 Bitmap,再在其基礎上生成新圖片。這種做法效率相對較低,因為原圖已經完整載入記憶體。
從 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 的做法雖然直觀,卻因為需要先把原圖完整載入而顯得笨重。實際開發中,建議根據圖片大小與場景需求靈活取捨。
我們為什麼要在 Android 中使用 RxJava
近來 RxJava 風生水起,不學一下都不好意思了。本人也是初學 RxJava,起初還覺得代碼好像更複雜、更難懂了,直到讀到一篇外文深有共鳴,便簡單翻譯過來分享給大家。本
自動填入簡訊驗證碼:監聽廣播、解析內容到回填輸入框
「輸入驗證碼」這個環節,如果每次都要跳出簡訊 App 記下六個數字再切回來,體驗相當糟。Android 可以監聽簡訊接收廣播,從內容中擷取數字後直接回填。以下拆解四個步驟
Android Studio Lint 使用指南:靜態檢查、自定義提示與程式碼品質
Lint 是什麼 Android Studio 內建 Lint 靜態分析工具,可協助開發者找出專案中的潛在問題,例如硬編碼字串、未使用資源、拼字錯誤、可能的 Null 問
HyBrid 混合開發初探:原生 WebView 與 JsBridge 雙方案對比
最近研究了 HyBrid 的兩種實現路徑:一是直接用原生 WebView 的接口能力,二是引入第三方 JsBridge 庫。本文把兩者的用法完整跑一遍,便於對照取捨。