I'm trying to make a gradient that emits from the middle of the screen in white, and turns to black as it moves toward the edges of the screen.
As I make a "normal" gradient like this, I have been experimenting with different shapes:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#E9E9E9" android:endColor="#D4D4D4"
android:angle="270"/>
</shape>
When using the "oval"-shape I at least got a round shape, but there were no gradient effect. How can I achieve this?'
You can also do it in code if you need more control, for example multiple colors and positioning. Here is my Kotlin snippet to create a drawable radial gradient:
Basic usage (but feel free to adjust with additional params):
Here is the complete xml with gradient, stoke & circular shape.
You can get a circular gradient using
android:type="radial"
:I always find images helpful when learning a new concept, so this is a supplemental answer.
The
%p
means a percentage of the parent, that is, a percentage of the narrowest dimension of whatever view we set our drawable on. The images above were generated by changing thegradientRadius
in this codemy_gradient_drawable
Which can be set on a view's
background
attribute like thisCenter
You can change the center of the radius with
where the decimals are fractions of the width and height for
x
andy
respectively.Documentation
Here are some notes from the documentation explaining things a little more.
I guess you should add android:centerColor
This example displays a horizontal gradient from white to black to white.