How to set gradient style to paint object?

Rohan K picture Rohan K · Dec 22, 2011 · Viewed 31.9k times · Source

The code for drawing an arrow with Style: Fill is given below:

paint.setColor(Color.parseColor("#bdc0dc"));
paint.setStyle(Style.FILL);
canvas.drawPath(arrowPath, paint);
paint.setColor(Color.BLACK);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
canvas.drawPath(arrowPath, paint);

And the output I obtain is this :

enter image description here

Now what I want do is set style to Gradient(Style.gradient not present in android...) to obtain the arrow similar to the image given below :

enter image description here

How do i do it ? I tried adding style in style.xml but was unable to add gradient there as it accepts item as parameter..

Answer

Sujit picture Sujit · Dec 22, 2011

use the code below..

paint.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));
    canvas.drawPath(arrowPath, paint);