Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to draw a circle with radial gradient in a canvas?
    primarykey
    data
    text
    <p>I created a circle button that can change his color when I call a function. What I want is to create another one, that creates the same circle button but with a radial gradient that starts in the middle with the color selected and that goes to transparent when you go out of the circle.</p> <p>I created a similar code using the one posted at <a href="https://stackoverflow.com/questions/8600805/how-to-set-gradient-style-to-paint-object">How to set gradient style to paint object?</a> but don't worked.</p> <p>The code that I tried is to this porpuse is:</p> <pre><code>mPaint.setShader(new RadialGradient(0, 0, height/3, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.MIRROR)); </code></pre> <p>The following class is the one that I created for a Circle Button.</p> <pre><code>public class ColorGradientCircleButton extends View{ private Paint mPaint; private Paint mBitmapPaint; private Bitmap mBitmap; private Canvas mCanvas; private int width, height; public ColorGradientCircleButton(Context context) { super(context); init(); } public ColorGradientCircleButton(Context context, AttributeSet attrs) { super(context, attrs); init(); } public ColorGradientCircleButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { mPaint = new Paint(); mPaint.setColor(Color.BLACK); mPaint.setStrokeWidth(1); mPaint.setStyle(Paint.Style.FILL_AND_STROKE); mBitmapPaint = new Paint(Paint.DITHER_FLAG); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); width = w; height = h; mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); mCanvas.drawCircle(w/2, h/2, h/3, mPaint); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); } public void changeColor(int color){ mPaint.setColor(color); mCanvas.drawCircle(width/2, height/2, height/3, mPaint); invalidate(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload