2

我正在尝试绘制一个带有黑色阴影边框的环。当我使用像 RED 这样的任何颜色时,我可以通过自定义视图来实现这一点,但我想要的是一个带有黑色阴影边框的透明圆圈。

Paint mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setColor(Color.RED);
    mPaint.setShadowLayer(5.5f, 6.0f, 6.0f, Color.BLACK);

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawCircle(70, 70, 50, mPaint); }
4

3 回答 3

3

这里发生的是它正在绘制一个填充的透明圆圈,并将阴影层设置为那个。由于圆形是透明的,您可以通过它看到对象的整个阴影,而不仅仅是您正在寻找的边缘。阴影是黑色的,所以看起来整个圆圈都是黑色的。

尝试将 Paint 样式设置为 Stroke。那应该让中间透明,只画外环的阴影。它可能会在两个方向(内部和外部)绘制阴影,因此您可能需要相应地调整阴影半径。

mPaint.setStyle(Paint.Style.STROKE);
于 2012-09-11T14:21:29.390 回答
0

You'll need to effectively draw the shadow using a fully transparent colour, as you've found so far, then remove that coloured center using PorterDuff.Mode.CLEAR - leaving just the shadow on the outside.

PorterDuffXfermode mXferMode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);

// draw the src/dst example into our offscreen bitmap
int sc = canvas.saveLayer(0, 0, 70 + 50, 70 + 50, null,
                                      Canvas.MATRIX_SAVE_FLAG |
                                      Canvas.CLIP_SAVE_FLAG |
                                      Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
                                      Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
                                      Canvas.CLIP_TO_LAYER_SAVE_FLAG);

canvas.drawCircle(70, 70, 50, shadowPaint);
shadowPaint.setXfermode(mXferMode);
canvas.drawCircle(70, 70, 50, shadowPaint);
shadowPaint.setXfermode(null);
canvas.restoreToCount(sc);
于 2013-09-20T21:31:36.487 回答
0

您的目标是 Android SDK 11 或更高版本吗?由于带有硬件支持的 HoneyComb 阴影渲染已被禁用,因此您必须为该层打开软件渲染。你必须注释你的函数来设置这样的绘画:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setUpPaint(){
    mPaint.setAntiAlias(true);
    mPaint.setColor(Color.RED);
    mPaint.setShadowLayer(5.5f, 6.0f, 6.0f, 0x80000000);

    /* --- for android:minSdkVersion="11" --- */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    setLayerType(LAYER_TYPE_SOFTWARE, mPaint);
}
}
于 2013-02-10T17:32:25.093 回答