如何在Android中的画布上绘制平滑/抖动渐变

时间:2023-01-22 20:17:00

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.setDither(true)在Android中绘制平滑渐变。这对我的代码没有任何影响。知道我需要改变什么才能在我的动态壁纸中获得漂亮的渐变效果吗?

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);

1 个解决方案

#1


71  

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

看到你有一个Canvas可以使用。这是一个选择。

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:

结果:

如何在Android中的画布上绘制平滑/抖动渐变

#1


71  

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

看到你有一个Canvas可以使用。这是一个选择。

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:

结果:

如何在Android中的画布上绘制平滑/抖动渐变