怎样消除锯齿的Andr​​oid的画布上剪辑的边界?

怎样消除锯齿的Andr​​oid的画布上剪辑的边界?

问题描述:

我使用的是Android的android.graphics.Canvas类to画一个圈。我的OnDraw方法剪辑的画布,使为内圆一个孔,然后在孔上绘制完整的外圆:

I'm using Android's android.graphics.Canvas class to draw a ring. My onDraw method clips the canvas to make a hole for the inner circle, and then draws the full outer circle over the hole:

    clip = new Path();
    clip.addRect(outerCircle, Path.Direction.CW);
    clip.addOval(innerCircle, Path.Direction.CCW);

    canvas.save();
    canvas.clipPath(clip);
    canvas.drawOval(outerCircle, lightGrey);
    canvas.restore();

其结果是一个pretty的,反锯齿外边缘锯齿状,难看的内缘的环:

The result is a ring with a pretty, anti-aliased outer edge and a jagged, ugly inner edge:

别名

我能做些什么来消除锯齿的内边缘?

What can I do to antialias the inner edge?

我不希望在中间画一个灰色圆圈,因为对话是微透明作弊。 (此透明度不上其他的背景微妙。)

I don't want to cheat by drawing a grey circle in the middle because the dialog is slightly transparent. (This transparency isn't as subtle on on other backgrounds.)

AFAIK,你不能抗混叠剪辑区。

AFAIK, you can't antialias clip regions.

我建议使用位图屏蔽代替。渲染粉红色,白色和浅灰色前景的一个位图,使得外/内圆面膜(灰度alpha通道)到另一个位图,然后用 Paint.setXfermode 渲染前景位图用面膜为Alpha通道。

I'd suggest using bitmap masking instead. Render the the pink, white, and light gray foreground to one bitmap, render the outer/inner circle mask (the grayscale alpha channel) to another bitmap, and then use Paint.setXfermode to render the foreground bitmap with the mask as its alpha channel.

这是例子可以在 ApiDemos 源$ C ​​$ C在这里找到:

An example can be found in the ApiDemos source code here:

  • http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html

修改:良好的措施,这里有一个屏幕截图中的API演示的活动,说明了屏蔽:

EDIT: for good measure, here's a screenshot of that activity in API Demos, illustrating masking:

Xfermodes截图