5

You can generate circular gradient with the following code:

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

    <gradient
        android:centerColor="#c1c1c1"
        android:endColor="#4f4f4f"
        android:gradientRadius="400"
        android:startColor="#c1c1c1"
        android:type="radial" >
    </gradient>
   
</shape>

But how would one draw elliptical gradient in android? I.e. different radius for X and Y?

fdermishin
  • 3,519
  • 3
  • 24
  • 45
Buddy
  • 2,074
  • 1
  • 20
  • 30

1 Answers1

0

You should be able to wrap your gradient drawable inside a scale drawable with unequal width and height scaling to get the effect of different X and y radii:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/gradientShape"
    android:scaleGravity="center_vertical|center_horizontal"
    android:scaleHeight="80%"
    android:scaleWidth="100%" />
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thank you for your answer, but as you might know, scale drawable is not visible due to level issue. Setting its drawable level to 1 to make it visible doesn't work on Android ICS for some reason. Do you have any workaround for this? – Buddy Aug 28 '15 at 17:13
  • @EnesBattal - I don't understand your comment. Drawing levels have to do with [`LevelListDrawable`](http://developer.android.com/guide/topics/resources/drawable-resource.html#LevelList), not `ScaleDrawable`. If you're referring to API level, `ScaleDrawable` has been supported since API level 1. – Ted Hopp Aug 28 '15 at 17:56
  • But there is an issue with ScaleDrawable afaik, if it doesn't have a level it doesn't show see http://stackoverflow.com/questions/5507539/android-scaledrawable-doesnt-seems-to-work – Buddy Aug 29 '15 at 18:49
  • @EnesBattal - Hm. I can't say that I've encountered that problem. From the thread you reference, it seems that the issue is with the level of the drawable that is wrapped by the `ScaleDrawable`, not the level of the `ScaleDrawable` itself. I suppose you could wrap your gradient in a level list drawable and then wrap that in a scale drawable. Then you could set the level to of the level list drawable 1 (using the `getDrawable()` method of `ScaleDrawable`. – Ted Hopp Aug 30 '15 at 00:47