透明圆圈内的三角形如何覆盖圆圈的颜色

时间:2013-04-04 14:44:48

标签: android colors android-drawable shapedrawable

我必须绘制一个自定义的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 );

}

BTW我正在覆盖ImageView!

2 个答案:

答案 0 :(得分:1)

不是试图绘制三角形,而是如何裁剪画布?

/* set up triangle path... */
path.close();
canvas.save();
canvas.clipPath(path, Region.Op.XOR);
canvas.drawCircle(...);
canvas.restore();

答案 1 :(得分:0)

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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    setLayerType(LAYER_TYPE_SOFTWARE, null);
}