I want to draw a custom shape like this-.
One option is to make each shape in photoshop individually and then use it in coding but I want to know that is it possible to draw this using xml?
How should I draw the shape like this? Don't expect complete code, just give me idea or point me in the right direction.
Override onDraw() The parameter to onDraw() is a Canvas object that the view can use to draw itself. The Canvas class defines methods for drawing text, lines, bitmaps, and many other graphics primitives. You can use these methods in onDraw() to create your custom user interface (UI).
Try the following shape drawable xml:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Colored rectangle-->
<item>
<shape android:shape="rectangle">
<size
android:width="100dp"
android:height="40dp" />
<solid android:color="#FAD55C" />
</shape>
</item>
<!-- This rectangle for the left side -->
<!-- Its color should be the same as layout's -->
<item
android:right="90dp"
android:left="-30dp">
<rotate
android:fromDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
<!-- These rectangles for the right side -->
<!-- Their color should be the same as layout's -->
<item
android:top="-40dp"
android:bottom="63dp"
android:right="-25dp">
<rotate
android:fromDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
<item
android:top="63dp"
android:bottom="-40dp"
android:right="-25dp">
<rotate
android:fromDegrees="-45">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
</layer-list>
That's how it looks like on a white background:
Here is more info on Shape Drawables.
EDIT:
The following is a small explanation of how it's done.
top, bottom, right and left
attributes of the size
tag of shape means the distance from the shape's borders to top, bottom, right and left
edges of the container (yellow rectangle).For example, if we want to place a smaller rectangle inside of the yellow one with a 10dp
gap from each yellow rectangle's edge we'd set the top, bottom, right and left
attributes equal to 10dp
for the new (inner) rectangle.
size
tag attribute's values can be negative which means that the corresponding edge of the shape appears outside of the container. In the previous example, if you set, say, the left
attribute to 100 dp or higher, the inner rectangle won't show up, because it'll be behind the right boarder of the yellow one.Regarding rotation, it goes clockwise for positive values and counterclockwise otherwise.
Hopefully, this didn't confuse you.
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