Search code examples
androidgraphicsgradientradial-gradients

Android smooth gradient


Am drawing a simple circle in a Canvas on Android. Now I need to fill it with some gradient colors, but they are no really smooth. This its what i have done:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    rectF.set(20, 20, this.get_Width() - this.get_Width()/10, this.get_Width() - this.get_Width()/10);
    RadialGradient gradient = new RadialGradient(200, 200, 200, 0xFFFFFFFF,
            0xFF000000, android.graphics.Shader.TileMode.CLAMP);
    Paint mPaint = new Paint();
    mPaint.setDither(true);
    mPaint.setShader(gradient);
    canvas.drawCircle(getWidth()/2, getHeight()/2, getWidth()/3, getGradient());
    invalidate();
}

And this is the result:

readial gradient

My question is: Is it there some way to make HQ radial gradients?


Solution

  • Finally I should initialize my Paint object as:

    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    

    That makes the borders cleaner.

    And with the @Waqas answer, make the colors itself smoothers.