public static Bitmap tile ( final Bitmap bitmap , final int width , final int height ) { Condition . INSTANCE . ensureNotNull ( bitmap , "The bitmap may not be null" ) ; Condition . INSTANCE . ensureAtLeast ( width , 1 , "The width must be at least 1" ) ; Condition . INSTANCE . ensureAtLeast ( height , 1 , "The height must be at least 1" ) ; Bitmap result = Bitmap . createBitmap ( width , height , Bitmap . Config . ARGB_8888 ) ; Canvas canvas = new Canvas ( result ) ; int originalWidth = bitmap . getWidth ( ) ; int originalHeight = bitmap . getHeight ( ) ; for ( int x = 0 ; x < width ; x += originalWidth ) { for ( int y = 0 ; y < width ; y += originalHeight ) { int copyWidth = ( width - x >= originalWidth ) ? originalWidth : width - x ; int copyHeight = ( height - y >= originalHeight ) ? originalHeight : height - y ; Rect src = new Rect ( 0 , 0 , copyWidth , copyHeight ) ; Rect dest = new Rect ( x , y , x + copyWidth , y + copyHeight ) ; canvas . drawBitmap ( bitmap , src , dest , null ) ; } } return result ; }
Creates and returns a bitmap with a specific width and height by tiling another bitmap .