Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Draw shape with skewed corner

I want to use a background for my buttons. But when I use a png it slows down the app. Therefore I want to use a xml shape but I do not know how to make the corner cut (like on the picture). enter image description here

By now I have the following shape which is just a rectangle:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/blue_semi_transparent"/>
    <padding android:bottom="10dp" android:right="10dp" android:top="10dp" android:left="10dp"/>
    <margin android:bottom="10dp" android:right="10dp" android:top="10dp" android:left="10dp"/>
</shape>

How can I draw the bottom right corner?

like image 835
Kewitschka Avatar asked Jun 15 '17 09:06

Kewitschka


2 Answers

You can try this,

skewed_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >


            <solid android:color="#3F8EEB" />
        </shape>
    </item>
    <item>
        <rotate
            android:fromDegrees="110"
            android:pivotX="90%"
            android:pivotY="90%"
            >
            <shape android:shape="rectangle" >
                <solid android:color="#FFF" />
            </shape>
        </rotate>
    </item>

</layer-list>

sample_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical" >

    <View
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:background="@drawable/triangle1"/>

</LinearLayout>

If you want to modify the skewed area you just need to change the value of fromDegrees, pivotX and pivotY value in the second item in skewed_background.xml.

like image 74
Muthukrishnan Rajendran Avatar answered Sep 20 '22 04:09

Muthukrishnan Rajendran


This is exactly what you looking for

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg"
        android:paddingRight="15dp"
        android:textColor="#ffffff"
        android:layout_centerInParent="true"

        android:text="Button" />
</RelativeLayout>

bg.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


    <item>
        <shape android:shape="rectangle">
            <size
                android:width="100dp"
                android:height="40dp" />
            <solid android:color="#13a89e" />
        </shape>
    </item>


    <item
        android:right="100dp"
        android:left="100dp"
        android:top="-100dp"
        android:bottom="-100dp">
        <rotate
            android:fromDegrees="45">
            <shape android:shape="rectangle">
                <solid android:color="#ffffff" />
            </shape>
        </rotate>
    </item>


    <item
        android:right="-100dp"
        android:left="100dp"
        android:top="-100dp"
        android:bottom="-100dp">
        <rotate
            android:fromDegrees="45">
            <shape android:shape="rectangle">
                <solid android:color="#ffffff" />
            </shape>
        </rotate>
    </item>

</layer-list>

OUTPUT

enter image description here

like image 33
Aditya Vyas-Lakhan Avatar answered Sep 20 '22 04:09

Aditya Vyas-Lakhan