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
Related
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.
I'm attempting to define a custom button shape in XML using a layer-list in order to avoid adding yet more bitmaps to the project.
I have this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:id="#+id/tester"
>
<rotate
android:pivotX="20%"
android:fromDegrees="-45"
>
<shape
android:shape="rectangle">
<solid
android:color="#000000"
>
</solid>
<size
android:height="200dp"
android:width="200dp"
>
</size>
</shape>
</rotate>
</item>
<item android:id="#+id/tester"
>
<rotate
android:pivotX="20%"
android:fromDegrees="45"
>
<shape
android:shape="rectangle">
<solid
android:color="#000000"
>
</solid>
<size
android:height="200dp"
android:width="200dp"
>
</size>
</shape>
</rotate>
</item>
<item>
<rotate
android:pivotX="90%"
android:fromDegrees="270"
>
<shape
android:shape="rectangle"
>
<solid
android:color="#000000"
>
</solid>
<size
android:width="50dp"
android:height="100dp"
>
</size>
</shape>
</rotate>
<item>
<rotate
android:pivotX="90%"
android:fromDegrees="90"
>
<shape
android:shape="rectangle"
>
<solid
android:color="#000000"
>
</solid>
<size
android:width="50dp"
android:height="100dp"
>
</size>
</shape>
</rotate>
</item>
Which creates this image:
And I'm using this to set it to a button:
<Button
android:layout_width="15dp"
android:layout_height="fill_parent"
android:id="#+id/picture_inflator"
android:layout_toLeftOf="#id/picture_fragment"
android:layout_above="#+id/button"
android:layout_below="#+id/title"
android:background="#drawable/rightButton"
android:layout_marginLeft="4dp"
/>
But the image this is creating in the button is this:
I have no idea what's going on here. I've drawn the images out wondering if it was including the parts of the shapes that had fallen off of the bounds but it doesn't look like that. I can't fix this until I know why its acting like this. I would greatly appreciate any insights or guesses. Thanks
EDIT: I'm aware that i'm supposed to use a selector here, but I'm just testing this right now. The image won't be matte black when its really done.
I think it is because you are using large sizes like 200dp. Try scaling everything down to the size you want the actual button to be.
If that doesn't work see if you can set the android:scaleType attribute on the button to something like android:scaleType="fitXY"
First I'd like to thank everyone that tried to help. Turns out that YoungCoconut was on the right track. When I set the the items' sizes to more appropriately reflect the size they were being scaled to the image shape became just like what I was actually seeing. So it appears that even portions of the object that have been cut off are still saved. More than likely the render doesn't use the image in the preview pane at all, but instead builds the object one item at a time while scaling. I've resorted to using Krita to build the objects and loading them into the project.
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
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've created a button, when it's clicked, it's like the picture below.
For now, it's an image created with Gimp and 9-patch. It works, but I want to know if there is a way to create the same image with a Shape in XML (only the blue part in the middle). So in the future, I can change for example the color very easily.
Thanks in advance.
Edit :
I tried something :
<item >
<shape android:shape="rectangle">
<solid android:color="#color/blue1"/>
</shape>
</item>
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<solid
android:color="#android:color/transparent" />
</shape>
</rotate>
</item>
But, of course, it doesn't work. The transparent triangle just show the blue rectangle in the background. Maybe with two triangle and one square, but i'm really weak in android shape system.