0

我必须绘制一个由圆圈内的三角形组成的自定义 shapeDrawable。我可以画圆和三角形,问题是我希望三角形是透明的,但是当我为绘制三角形的路径赋予透明颜色时,我会在三角形中看到圆圈的颜色,我不想有颜色圆形作为三角形的背景?这是我正在使用的代码:

    @Override
    protected void onDraw(Canvas canvas) {

    Paint p = new Paint ();

    int x = 1;
    int y = 1;
    Rect bounds = canvas.getClipBounds ();


    p.setColor (color.getBackgroundColor());
    p.setStyle(Style.FILL);
    p.setStrokeWidth (0);

    Path path = new Path();
    Point p1 = new Point(bounds.centerX()-bounds.height()/4, bounds.centerY()-bounds.height()/4);
    Point p2 = new Point(bounds.centerX()-bounds.height()/4, bounds.centerY()+bounds.height()/4);
    Point p3 = new Point(bounds.centerX()+bounds.height()/4, bounds.centerY());
    path.moveTo(p1.x, p1.y);
    path.lineTo(p2.x, p2.y);
    path.lineTo(p3.x, p3.y);
    path.close();

    Paint pTriang = new Paint();
    pTriang.setColor(Color.TRANSPARENT);
    pTriang.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR)); 
    canvas.drawCircle(bounds.exactCenterX(), bounds.exactCenterY(), bounds.height()/2, p);
    canvas.drawPath(path, pTriang );

}

顺便说一句,我正在覆盖 ImageView!

4

2 回答 2

1

与其尝试绘制三角形,不如剪裁画布?

/* set up triangle path... */
path.close();
canvas.save();
canvas.clipPath(path, Region.Op.XOR);
canvas.drawCircle(...);
canvas.restore();
于 2013-04-04T14:50:18.560 回答
0

我认为您需要在 Honeycomb 及更高版本上将图层类型设置为软件。在构造函数的末尾添加:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    setLayerType(LAYER_TYPE_SOFTWARE, null);
}
于 2013-04-04T19:05:29.477 回答