我們可以使用Matrix 來放縮我們得到的Bitmap 從而使我們的BItmap適應我們的手機屏幕
首先我們得先獲取我們的手機屏幕的大小
WindowManager wm = (WindowManager) getContext().getSystemService( Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight();
然后我們構造一個新的Matrix對象,自己完成寫一個函數,如下:
public Bitmap resizeBitmap(Bitmap bitmap,int w,int h) { if(bitmap!=null) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int newWidth = w; int newHeight = h; float scaleWight = ((float)newWidth)/width; float scaleHeight = ((float)newHeight)/height; Matrix matrix = new Matrix(); matrix.postScale(scaleWight, scaleHeight); Bitmap res = Bitmap.createBitmap(bitmap, 0,0,width, height, matrix, true); return res; } else{ return null; } }
這樣我們通過這個函數返回的Bitmap對象就是可以適應我們手機屏幕大小的了。。