Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created July 5, 2011 11:40
Show Gist options
  • Save codeswimmer/1064692 to your computer and use it in GitHub Desktop.
Save codeswimmer/1064692 to your computer and use it in GitHub Desktop.
Android: How to display a tiled bitmap background
// ============================================================================================
// Code
//
// Example showing how to set a View's background to a tiled bitmap.
// assumes: res/drawable/pattern01.png - The bitmap representing an individual tile
public void setViewTiledBackground(View view, Resources resources) {
Bitmap tile = BitmapFactory.decodeResource(resources, R.drawable.pattern02);
BitmapDrawable tiledBitmapDrawable = new BitmapDrawable(resources, tile);
tiledBitmapDrawable.setTileModeX(Shader.TileMode.REPEAT);
tiledBitmapDrawable.setTileModeY(Shader.TileMode.REPEAT);
view.setBackgroundDrawable(tiledBitmapDrawable);
}
// ============================================================================================
// XML
//
// Example shows how to display a tiled image as a ViewGroup's background.
assumes: res/drawable/pattern01.png - The bitmap representing an individual tile
/* res/layout/main.xml ************************************************************************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/tiled">
</LinearLayout>
***********************************************************************************************/
/* res/layout/drawable/tiled.xml **************************************************************
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/pattern01"
android:dither="true"
android:tileMode="repeat"
android:dither="true"
/>
***********************************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment