發表於09月27日 10:10圖片平鋪的三種方式:
1)第一種利用系統提供的api實現
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.pic); //bitmap = Bitmap.createBitmap(100, 20,Config.ARGB_8888); BitmapDrawable drawable = new BitmapDrawable(bitmap); drawable.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT); drawable.setDither(true); view.setBackgroundDrawable(drawable); Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.pic); //bitmap = Bitmap.createBitmap(100, 20,Config.ARGB_8888); BitmapDrawable drawable = new BitmapDrawable(bitmap); drawable.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT); drawable.setDither(true); view.setBackgroundDrawable(drawable);
2)第二種我們使用xml來輕鬆實現
< bitmapxmlns:android="http://schemas.android.com/apk/res/android"android:src="@drawable/img" android:tileMode="repeat" />
3)第三種自己畫出來
public static Bitmap createRepeater(int width,Bitmap src){ int count = (width + src.getWidth() - 1) /src.getWidth(); Bitmap bitmap = Bitmap.createBitmap(width, src.getHeight(),Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); for(int idx = 0; idx < count; ++ idx){ canvas.drawBitmap(src, idx * src.getWidth(), 0, null); } return bitmap; } public static Bitmap createRepeater(int width, Bitmapsrc){int count = (width + src.getWidth() - 1) / src.getWidth(); Bitmap bitmap = Bitmap.createBitmap(width, src.getHeight(),Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); for(int idx = 0; idx < count; ++ idx){ canvas.drawBitmap(src, idx * src.getWidth(), 0, null); } return bitmap; }
前兩種偶爾會出現bug,平鋪有問題,還是最後一種比較使用,呵呵!