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

╄→尐↘猪︶ㄣ 提交于 2019-12-17 17:28:58

问题


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

回答1:


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:



来源:https://stackoverflow.com/questions/2936803/how-to-draw-a-smooth-dithered-gradient-on-a-canvas-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!