在設置背景圖時,如果圖片不夠大會被拉伸,使圖片失真,如果圖片太大會對view控件的顯示造成影響。
如果只是在ImageView中設置圖片的話,在程式中可以利用setScaleType進行動態設定,在xml中可以簡單的用android:scaleType來設定。
(android:scaleType="CENTER_INSIDE" 圖片比View小,圖片不會拉伸
android:scaleType="CENTER_CROP" 圖片比View大,View不被拉伸 其他屬性的設置以后慢慢在研究。)
現在要作的是設置LinearLayout的background時如何讓圖片自適應屏幕的大小,包含屏幕旋轉時的調整。
程式代碼如下:
private Drawable createImage(File imageFile) { try { // 取得當前屏幕的長寬 DisplayMetrics dm = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(dm); float screenWidth = dm.widthPixels; float screenHeight = dm.heightPixels; // 取得圖片的大小並計算縮放比例 BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(imageFile), null, o); float bitmapWidth = o.outWidth; float bitmapHeight = o.outHeight; float scale = (screenWidth / bitmapWidth < screenHeight / bitmapHeight) ? screenWidth / bitmapWidth : screenHeight / bitmapHeight; // 圖片縮小放大 Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream( imageFile)); Matrix matrix = new Matrix(); matrix.postScale(scale, scale); Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); bitmap.recycle(); // 繪制背景圖片 Bitmap mBitmap = Bitmap.createBitmap((int) screenWidth, (int) screenHeight, Bitmap.Config.RGB_565); Canvas mCanvas = new Canvas(mBitmap); Paint BitmapPaint = new Paint(Paint.FILTER_BITMAP_FLAG); // 設定背景顏色 mCanvas.drawColor(0xff000000); mCanvas.drawBitmap(resizedBitmap, screenWidth / 2 - bitmapWidth * scale / 2, screenHeight / 2 - bitmapHeight * scale / 2, BitmapPaint); mCanvas.save(); BitmapDrawable drawable = new BitmapDrawable(mBitmap); resizedBitmap.recycle(); return drawable; } catch (FileNotFoundException e) { } return null; }
調用方法為:
try { Drawable image = createImage(imageFile); background.setBackgroundDrawable(image); } catch (Exception e) { Log.e("Exception", e.toString()); return false; }
//===========================================================================
在上面的程式中用到了Matrix進行圖片的放大和縮小。
使用BitmapFactory.decodeStream的option的話只能放大或縮小整數倍(使用方法在其他文章中有介紹了)
Matrix的功能非常強大,不止可以放大縮小,還可以設置透明度等,Matrix的操作,總共分為translate(平移),rotate(旋轉),scale(縮放)和skew(傾斜)四種。
具體使用方法參考網址:
http://blog.csdn.net/hui_ttoo/article/details/6202762
http://liliang1222.iteye.com/blog/1152474
http://www.moandroid.com/?p=1781
http://www.cnblogs.com/leon19870907/articles/1978065.html
http://www.r-base.net/archives/148
http://my.oschina.net/amigos/blog/59598
// ==========================================================================
使用到了Canvas用來根據之前的圖片的縮放比例配合背景畫一張新的背景圖,用來顯示。
具體用法可以參考網址。
http://aina-hk55hk.iteye.com/blog/690921
// ==========================================================================
另外在查資料過程中,還發現另一種制作圖片效果的用法。
參考網址:http://blog.csdn.net/pgmsoul/article/details/7073332
BitmapDrawable drawable = new BitmapDrawable(mBitmap); drawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT); drawable.setDither(true);
可以實現圖片平鋪(TileMode.REPEAT)和倒影效果(TileMode.MIRROR),在setTileModeXY中設置不同的參數。
還有另外一種TileMode.CLAMP,邊緣拉伸效果,不知道在什么情況下會用到。
// =========================================================================
另外在制作背景圖時,可以利用draw9patch來制作不失真背景。
例如textView添加邊框,可以利用.9.png。
具體可以參考網址:http://archive.cnblogs.com/a/2017591/