The android framework allows you to create a drawable that contains a bitmap for tiled, scaled, and aligned processing. When we want the background to be tiled using the following image:
1) the first method is to use the API provided by the system.
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 );
The tilemode attribute defines the display mode of the background:
- Disabled
-
Default Value, indicating no tile is used
- Clamp
-
Copy edge color
- Repeat
-
Repeat the image display on the X and Y axes, that is, the tile we are talking about.
- Mirror
-
Use an alternate image in the horizontal and vertical directions to repeat the image painting.
2) The second method is to use XML for easy implementation.
<Bitmap xmlns: Android = "http://schemas.android.com/apk/res/android" Android: src = "@ drawable/IMG"
Android: tilemode = "repeat"/>
3) The third method is to draw it by yourself.
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;
}
The final tile effect is as follows:
Note: The first two may have bugs, and the third one is more practical. From: http://blog.csdn.net/t12x3456/article/details/7738475