Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to paint a gradient color in Android on a Canvas with the color changing?

I need to paint the background of my Canvas one color with shading/gradient applied, but each onDraw call I would like to potentially change the color.

I am having trouble doing this without creating a new object each onDraw call. Anyone have any ideas? If I use drawPaint() and set a new shader() to my paint, then I have created a new shader object and if I create a new GradientDrawable() I have as well. I want to avoid GC.

I thought I could reuse one GradientDrawable() object and call the .setColor() method, but this just resets any gradient data associated it and paints the drawable as that solid color.

Anyone?

like image 786
Greg Avatar asked Jan 18 '11 23:01

Greg


1 Answers

Unfortunately you have to create a new LinearGradient everytime. Note that you don't have to use a GradientDrawable, you can draw the shape yourself with a Paint on which you set the shader.

like image 59
Romain Guy Avatar answered Nov 16 '22 07:11

Romain Guy