Forked from codeswimmer/Android: Tiled Background
Created
September 30, 2023 07:29
-
-
Save vahid-m/ba6cc1c38f5bef8beebe27a10c44aaf0 to your computer and use it in GitHub Desktop.
Android: How to display a tiled bitmap background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============================================================================================ | |
// 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