Ivan Ivanov
Ivan Ivanov

Reputation: 61

Curved rectangle shape android

I want to create a rectangle shape in XML, it needs a curved shape. Is this possible with the XML markup or programmatically?

the code i have now:

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

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:topLeftRadius="45dp"
        android:topRightRadius="45dp"
        android:bottomLeftRadius="45dp"
        android:bottomRightRadius="45dp" />
    <solid
        android:color="#4F4F4F" />
    <stroke
        android:width="3dp"
        android:color="#878787" />
</shape>

Can someone help me out?

enter image description here

My idea is as followed:

https://i.sstatic.net/RD7I0.png

The rectangle has to be transformed with a curve.

Upvotes: 6

Views: 10651

Answers (1)

CSmith
CSmith

Reputation: 13458

You can use a shape resource for your view background, either an oval:

<shape android:shape="oval">
 <stroke android:width="1dp" android:color="#FF000000" />
</shape>

or a rectangle with rounded corners:

<shape android:shape="rectangle">
 <stroke android:width="1dp" android:color="#FF000000" />
 <corners android:radius="20dp" />
</shape>

it's not clear to me from your question what you're looking to do exactly. The oval will fit to your objects width and height.

Of course you can have precise control via a custom Drawable, implementing your own draw() method and painting on the canvas. This is likely your best approach if you're looking for something very specific.

Upvotes: 5

Related Questions