0

I know that there are many answers to this question, but I am looking for something different.

I know that by creating a gradient in xml, we can set the angle, but it should be a multiple of 45. Also, I know that there are no methods in the GradientDrawable class to set the angle.

So, my question is that how to set a custom angle of a gradient in android platform ? I want to set a custom gradient angle which may not be a multiple of 45.

Puspam
  • 2,137
  • 2
  • 12
  • 35
  • Possible duplicate of [angle attribute in android gradient](https://stackoverflow.com/questions/12153890/angle-attribute-in-android-gradient) – TeWu Mar 02 '19 at 11:14

1 Answers1

1

Here is how you can set angle in the background with this drawable code. But for that angle on the background, I will suggest a different way.

Way 1:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="#43CEE0"/>
        </shape>
    </item>

    <item
        android:bottom="-100dp"
        android:left="0dp"
        android:right="-260dp"
        android:top="270dp">
        <rotate
            android:fromDegrees="-10"
            android:pivotX="0%"
            android:pivotY="100%">
            <shape
                android:shape="rectangle">
                <solid
                    android:color="@android:color/white"/>
            </shape>
        </rotate>
    </item>
</layer-list>

Using this you can set the angle between view. But the angle will get disturbed on a different screen. When I use this on a big screen phone it simply not work effectively.

Another way is to get an image developed by the designer and set to the background. mdpi to xxxhdpi. In Android, you can refer above view to create an image.

Ankit Tale
  • 1,924
  • 4
  • 17
  • 30
  • You are trying to say that we need to use some workarounds because there is no inbuilt way to set a custom angle. – Puspam Mar 03 '19 at 09:01
  • What if I extend the gradient drawable class, set the angle and set it as background programmatically ? But I don't know how to do that. Please help a class 10 student. – Puspam Mar 03 '19 at 09:06
  • @PuspamAdak You might create a class extends GradientDrawable but I am damn sure you would need to handle many things regarding screen ratio. Lastly, I will tell you I you need to create a *PNG* and add to multiple drawable files for the different size. – Ankit Tale Mar 04 '19 at 12:17
  • Thanks for your suggestion. I will try to extend the GradientDrawable class. Meanwhile, I am accepting your layer list method because it seems to work quite well. Have a nice day :) – Puspam Mar 05 '19 at 08:11