I have this background for several LinearLayout
s 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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With