Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a smooth/dithered gradient on a canvas in Android

Several answers mention to use GradientDrawable.setDither(true) to draw smooth gradients in Android. That has no effect in my code. Any idea what I have to change to get a well looking gradient in my live wallpaper?

GradientDrawable gradient = new GradientDrawable(Orientation.TL_BR, colors); gradient.setGradientType(GradientDrawable.RADIAL_GRADIENT); gradient.setGradientRadius(canvas.getWidth() * 2); gradient.setDither(true); gradient.setGradientCenter(-0.1f, -0.1f); gradient.setBounds(cb); gradient.draw(canvas); 
like image 802
André Avatar asked May 29 '10 22:05

André


People also ask

How do you make a gradient on a canvas?

The addColorStop() method specifies the color stops, and its position along the gradient. Gradient positions can be anywhere between 0 to 1. To use the gradient, set the fillStyle or strokeStyle property to the gradient, then draw the shape (rectangle, text, or a line).

What is gradient drawable?

Designers are trying out different styles and combinations which go best with the Android App. One of the key components of Android being used these days is called GradientDrawable. A GradientDrawable is drawable with a color gradient for buttons, backgrounds, etc.


1 Answers

Seeing as you have a Canvas to work with. Here is one option.

private Bitmap makeRadGrad() {     RadialGradient gradient = new RadialGradient(200, 200, 200, 0xFFFFFFFF,             0xFF000000, android.graphics.Shader.TileMode.CLAMP);     Paint p = new Paint();     p.setDither(true);     p.setShader(gradient);      Bitmap bitmap = Bitmap.createBitmap(400, 400, Config.ARGB_8888);     Canvas c = new Canvas(bitmap);     c.drawCircle(200, 200, 200, p);      return bitmap; } 

Result:

Result

like image 120
Eric Schlenz Avatar answered Oct 08 '22 03:10

Eric Schlenz