ilomambo
ilomambo

Reputation: 8350

Android Making a drawable XML composed of several bitmaps

I have this background for several LinearLayouts throughout my app:

android:background="@drawable/metal_plate"

drawable\metal_plate.xml:

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/diamond_metal"
    android:tileMode="repeat"
    android:dither="true">
</bitmap>    

I would like to place 4 bitmaps of screws in the 4 corners of the metal plate.
I use the metal plate in several places, so I prefer to define it as a single drawable than having to place the 4 screws on every ocasion with a RelativeLayout.

Is it possible to define an XML in the drawable folder to combine the tiled metal plate and the 4 screws?

Upvotes: 9

Views: 14053

Answers (2)

Kevin Coppock
Kevin Coppock

Reputation: 134714

Unfortunately I can't really test this right now, but I believe you can pull this off with a LayerListDrawable like this:

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:src="@drawable/diamond_metal"
            android:tileMode="repeat" />
    </item>

    <item android:right="10dp" android:bottom="10dp">
        <bitmap
            android:src="@drawable/screw"
            android:gravity="bottom|right" />
    </item>

    <item android:left="10dp" android:bottom="10dp">
        <bitmap
            android:src="@drawable/screw"
            android:gravity="bottom|left" />
    </item>

    <item android:top="10dp" android:left="10dp">
        <bitmap
            android:src="@drawable/screw"
            android:gravity="top|left" />
    </item>

    <item android:top="10dp" android:right="10dp">
        <bitmap
            android:src="@drawable/screw"
            android:gravity="top|right" />
    </item>
</layer-list>

Replacing the 10dp values with whatever inset you need for the screws.

Upvotes: 27

Jwc24678
Jwc24678

Reputation: 161

This could probably be easily done using a NinePatch. You can create a NinePatch drawable then just set it as the background of any Layouts that you want to have the background. This just requires you create a square version of the background, then I would recommend using the Draw 9-Patch tool to make it into a .9.png for Android to use.

Upvotes: 1

Related Questions