Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

android - Gradient appears banded in a SurfaceView, but looks very smooth in a normal View

My problem is that when drawing a simple test image (a rectangle with a gradient fill) within a SurfaceView, the gradient has colour banding. When drawing exactly the same rectangle with the same gradient in a simple extended View, the gradient looks very smooth, and as nice as I would expect for 32-bit colour.

This is occurring side by side using the test SurfaceView placed alongside the test View within the same layout in the same Activity.

For information, in the Activity class I am calling window.setFormat(PixelFormat.RGBA_8888) to switch the Activity to 32-bit colour, as this was the solution in the past to eliminate banding in gradients (however, I believe that this is no longer necessary at a certain Android level, where 32-bit is the default). My minSdkVersion is 8.

The test code that appears in the onDraw() of both the simple extended View and the SurfaceView is:

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setAntiAlias(false);
    paint.setFilterBitmap(false);
    paint.setDither(false); 
    Shader shader = new LinearGradient(
            0,
            0,
            0,
            200,
            new int[]{0xff150d2f,0xff432b96},
            null,
            Shader.TileMode.CLAMP
            );

    paint.setShader(shader);    
    canvas.drawRect(0,0,getWidth(),getHeight(), paint);

Here is the screenshot showing the SurfaceView above and the normal View beneath. I realise that the banding is very subtle in this example; it's even more apparent when the gradient sweeps over a smaller band of colours.

Screenshot

Any suggestions, please?

Trev

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Found the solution myself in the end by adding this into the SurfaceView's constructor:

getHolder().setFormat(PixelFormat.RGBA_8888);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...