FelipeDev.-
FelipeDev.-

Reputation: 3133

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?

Upvotes: 4

Views: 2552

Answers (2)

FelipeDev.-
FelipeDev.-

Reputation: 3133

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.

Upvotes: 1

waqaslam
waqaslam

Reputation: 68167

Try adding the following to your Activity which contains the View that does the drawing:

@Override
public void onAttachedToWindow() {
   super.onAttachedToWindow();
   Window window = getWindow();
   // Eliminates color banding
   window.setFormat(PixelFormat.RGBA_8888);
}

Upvotes: 1

Related Questions