Android smooth gradient

I draw a simple circle in Canvas on Android. Now I need to fill it with gradient colors, but they are not very smooth. This is what I did:

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 there a way to make HQ radial gradients?

+4
source share
2 answers

Finally, I have to initialize my Paint object as:

 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); 

This makes the borders cleaner.

And with @Waqas answer make the colors smoother.

+1
source

Try adding the following to your Activity containing the View that makes the drawing:

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

Source: https://habr.com/ru/post/1435961/


All Articles