Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 492
ilomambo Avatar asked Mar 07 '13 18:03

ilomambo


People also ask

What is drawable XML file?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

What is stroke in android XML?

Sometimes you want an outline around your shape and to do that you can use the stroke tag. You can specify the width and color of the outline using android:width and android:color.


2 Answers

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.

like image 82
Kevin Coppock Avatar answered Oct 15 '22 23:10

Kevin Coppock


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.

like image 24
Jwc24678 Avatar answered Oct 16 '22 01:10

Jwc24678