I a created triangle shape following this post
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="#color/transparent" android:width="10dp"/>
<solid
android:color="#color/your_color_here" />
</shape>
</rotate>
</item>
But I have not been able to incline it towards the right or the left.
Any help is appreciated.
Thanks
I think you could create this layout by overlaying different shapes one after another like it is shown in the selected for best answer in this question Android: Drawing custom shapes First <item> could be your rectangle and then you could use one rectangle and one triangle for the pointer
Related
I am trying to create a Triangle on top of a Rectangular View in Android. I am using this drawable xml to create a Triangle View. But I am not able to get the rounded edge. How to achieve this. Can someone help ?
Xml code :
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="-135"
android:pivotX="90%"
android:pivotY="-45%">
<shape
android:shape="rectangle">
<solid
android:color="#android:color/black"/>
</shape>
</rotate>
</item>
What I am trying to achieve :
What I am getting in Device :
I have a task to create chat. Here is the UI:
I've tried to draw rectangle and triangle and in layer list to combine this 2 drawables, but it looks very bad...
Rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50dp"/>
<solid android:color="#color/message_cloud_client" />
<stroke android:color="#color/message_cloud_client_stroke" android:width="1dp"/>
</shape>
Triangle.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="90%" >
<shape
android:shape="rectangle" >
<stroke android:color="#android:color/transparent" android:width="10dp"/>
<solid
android:color="#dce7f7" />
</shape>
</rotate>
</item>
</layer-list>
You can do this using Nine-patch images instead of creating a layer-list. In a `Nine-patch' image you can indicate the rectangle that is grown or shrink when the image is resized. But the whole image is not affected. Here is the address of the tool:
sdk\tools\draw9patch
Open the file. Import your original image and set a rectangle inside the original images that is affected when the image is resized. (Do not include the edges of your picture).
Also you can do this with Vector images that is now available in support library.
The solutions I have found over internet cuts the rectangle but I am having like half of it without nothing and wasting space.
For example:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="#android:color/transparent" android:width="10dp"/>
<solid
android:color="#color/black" />
</shape>
</rotate>
</item>
</layer-list>
This code creates a triangle, but I have a lot of space wasted, look at the image:
I want to create a triangle that "fills" the image view. Something like this:
Not necessarily has to be equilateral. Any ideas?
Well, if it is okay for you to create the shape in java , you can do it by extending the Drawable class in Android. See this
I found several answers to How to draw a triangle shape in Android in the resources files. But I don't found any to explain how I can change the triangle rotation.
Example that I found:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke
android:color="#color/color_app_transparent"
android:width="10dp" />
<solid
android:color="#color/color_app_primary" />
</shape>
</rotate>
</item>
</layer-list>
Somebody can explain how can I change the Triangle Shape to do a arrow like this:
I manage to draw that arrow, here is the code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/triangle_shape">
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="0%"
android:pivotY="140%" >
<shape
android:shape="rectangle" >
<stroke
android:color="#color/color_app_transparent"
android:width="2dp" />
<size android:width="50dp" android:height="50dp"/>
<solid
android:color="#color/color_app_primary" />
</shape>
</rotate>
</item>
</layer-list>
For what I understand, I had to rotate the square 45º and then I tried to adjust the X and Y points to manage to hide most of the square and show only one edge and this way I made the arrow.
The shape after rotating 45º:
And then the final result after changing the points X and Y:
I used a ImageView to display my arrow with width and height as wrap_content.
I would like to draw a triangle on the upper right corner of a gridview item as a marker. I can do this with a background image but would like to use shape drawing with xml because this way things look more georgeous :D
Use below xml code
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="#color/transparent" android:width="10dp"/>
<solid
android:color="#color/your_color_here" />
</shape>
</rotate>
</item>
</layer-list>
There are already numerous resources available on the internet , However, the first link mentioned was helpful to me. However u can check below ones!
This link might help u out in achieving the goal.
Using Android Specification to draw triangle.
or drawing Triangle Using Canvas
Drawing Triangle Using Canvas