9

I need to draw a background of layout as a triangle, like you can see in the picture.

enter image description here

I've found an example where they do something similar, but I don't know how to adapt it to my case. Here is the example

Can anyone help me? Best.

Onik
  • 19,396
  • 14
  • 68
  • 91
aloj
  • 3,194
  • 7
  • 27
  • 38

1 Answers1

11

Here is an example of a drawable resource with a triangle in the right bottom corner in front of an image. As the image I used the default android launcher icon. You can use any other image.

Create triangle.xml file in res/drawable like this:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:drawable="@drawable/ic_launcher">
</item>
<item>
    <rotate
        android:fromDegrees="-45"
        android:toDegrees="0"
        android:pivotX="150%"
        android:pivotY="20%" >
        <shape
            android:shape="rectangle" >
            <solid android:color="#ff0000" />
        </shape>
    </rotate>
</item>
</layer-list>

Then use it as background attribute of a view:

android:background="@drawable/triangle"
Onik
  • 19,396
  • 14
  • 68
  • 91
  • 4
    @user1365554 I'll try to explain. PivotX (X goes from left to right) and pivotY (Y goes from up to down) are the coordinates of the center of rotation. E.g., if pivotX=0 and pivotY=0 then you rotate relative to left top corner of your shape. When you change the values you move the center of rotation. The fact is that it's not the kind of thing that can be easily understood, that's something you understand trying. So just try yourself and you'll get it. Some info can be found [here](http://developer.android.com/guide/topics/resources/animation-resource.html#View). – Onik Apr 16 '14 at 17:37
  • this creates a random figure, anything but a triangle – frankelot May 05 '15 at 01:26
  • @feresr The shape is to be used for square background. You should adjust the values in case of rectangle-shaped drawables. – Onik Aug 20 '15 at 14:12