Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radial gradient in XML with parent size

Im trying to achieve a component to make custom shadows to buttons or other components, i know that it will be easier with a 9patch or a png with the shadow, but i want to change it color and size programmatically also in its states (pressed,etc), so i decided to try with 9 images, all in XML so the shadow shades start its gradient from the side of the component.

<!-- Left Shadow layer -->
<item>
    <shape android:shape="rectangle" >
        <gradient
            android:angle="0"
            android:endColor="#FFFF0000"
            android:startColor="#00FF0000" />
    </shape>
</item>

It looks good, the problem is on the corners and with the android:gradientRadius parameter now its set to a fixed size, but in the contextual help is said that can be set in a percentage of the base size 10% or parent size 10%p, what i want its to set a 100%p radius so the gradient will always go from the main color and disappear in the edge of the square.

-- EDIT --

The android doc about gradientRadius gradientRadius

<shape android:shape="rectangle" >
        <gradient
            android:endColor="#00FF0000"
            android:startColor="#FFFF0000"
            android:gradientRadius="18"
            android:centerX="100%"
            android:centerY="100%"
            android:type="radial" />
    </shape>

And thats where im now :( i do not know how can i set this size to fit its parent view.

Any help will be appreciated, when im finished with the component i will put the code in an answer :) so typical buttons can have customizable shadows in xml.

An image of the deserved component.

--Edit--

Im still interested in this :) no one has a clue?

Button with radia gradients

like image 762
Goofyahead Avatar asked May 23 '12 16:05

Goofyahead


1 Answers

I think you should give up with xml and implement drawable in code. When you extend Drawable class you can get size as rectangle with getBounds(). Also you can dynamicaly recalculate in onBoundsChange method. You can also easily construct gradients and use them in Paint object (setShader method)

like image 135
snapix Avatar answered Nov 14 '22 12:11

snapix