tags:

views:

3299

answers:

4

I'm trying to load a bitmap in Android which I want to tile. I'm currently using the following in my view to display a bitmap:

canvas.drawBitmap(bitmap, srcRect, destRect, null)

I essentially want to use this bitmap as a background image in my app and would like to repeat the bitmap in both the X and Y directions.

I've seen the TileMode.REPEAT constant for the BitmapShader class but am unsure if this is to do with repeating he actual bitmap or to do with applying a filter to the bitmap.

+9  A: 

You would do this in the xml instead of the java code. I haven't attempted this myself but I did find this example.

<xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/MainLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/backrepeat"
>

then in an xml called backrepeat.xml

<bitmap android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/back" 
    android:tileMode="repeat" />

reference

mugafuga
plus one for helpfulness!
Ben Gotow
just to add, backrepeat.xml goes in the "drawable" folder
Ben Gotow
This wouldn't work for drawing to a canvas though...
stealthcopter
+3  A: 

The backrepeat.xml above is buggy

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/tile"
    android:tileMode="repeat"
    android:dither="true" />
Bo
+2  A: 

How about an in-code option? using android.graphics.Canvas in some way...

Beshoy Girgis
A: 

Anybody knows how to do this in java code without usage of xml?

endryha