Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oval Gradient in Android

I know how to setup and display an oval shape. I know how to apply a gradient to this shape. What I cant figure out is how I can get an oval gradient to match the shape.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval" >
    <gradient
        android:startColor="#66FFFFFF"
        android:endColor="#00FFFFFF"
        android:gradientRadius="100"
        android:type="radial" />
</shape>

If you can imagine, this gradient has a semi transparent white glow in the middle, then fades to alpha zero at the edges. I need to get it to go out in an oval shape, not just a circular gradient. How can I achieve this?

like image 840
coneybeare Avatar asked Aug 20 '10 01:08

coneybeare


People also ask

What is sweep gradient?

Sweep Gradient are mainly used for Rings. They are used for similar transactions as the Angular sweep (not sure about this one) which talks about transaction similar to sweep of a clock's hand. Very similar to Linear Gradients. The only thing I can think about for a difference between them is for the usage (like Rings) ...

What is gradientRadius?

gradientRadius is a property that is based on an integer value. Putting a percent or any kind of measurement value won't work.

What is GradientDrawable?

A GradientDrawable is drawable with a color gradient for buttons, backgrounds, etc.


2 Answers

I would suggest more 'direct' drawing approach. If you can draw gradient pixel-by-pixel, then you need just to remember that for

  • circle gradient color is proportional to r
  • ellipse (oval) gradient color is proportional to r1+r2

Here:

r - distance to circle center

r1,r2 - distances to two foci of ellipse

EDIT: Consider this Pixel Shader code:

uniform sampler2D tex;

void main()
{
    vec2 center = vec2(0.5,0.5);
    float len = 1.3*(distance(gl_TexCoord[0].xy,center));
    vec2 foc1 = vec2(len,0.);
    vec2 foc2 = vec2(-len,0.);
    float r = distance(center+foc1,gl_TexCoord[0].xy) +
             distance(center+foc2,gl_TexCoord[0].xy);
    float k = pow(r*0.9,1.3);
    vec4 color = vec4(k,k,k,1.);
    gl_FragColor = color;
}

You will get oval something like that: alt text

good luck

like image 179
Agnius Vasiliauskas Avatar answered Oct 06 '22 20:10

Agnius Vasiliauskas


<?xml version="1.0" encoding="utf-8"?>

<stroke android:width="1dp" android:color="#ffffffff" />

<size
    android:width="40dp"
    android:height="40dp"/>

<gradient
    android:type="radial"
    android:startColor="#ffff0000"
    android:endColor="#330000ff"
    android:gradientRadius="40dp"
    android:angle="270"
    android:centerX=".5"
    android:centerY=".5"/>

like image 26
yeahdixon Avatar answered Oct 06 '22 21:10

yeahdixon