0

I have to draw a custom shapeDrawable consisting of a triangle inside a circle. I can draw the circle and the triangle the question is that i want the triangle to be transparent but when i give a transparent color to the path drawing a triangle i will see the color of the circle insde the triangle i want to not have the color of the circle as backround of the triangle ? here's the code i'm using :

    @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 i'm overriding an ImageView!

Driss Bounouar
  • 3,182
  • 2
  • 32
  • 49
  • 1
    look at http://stackoverflow.com/questions/3874424/android-looking-for-a-drawarc-method-with-inner-outer-radius – ngesh Apr 04 '13 at 14:47
  • in that question they didn't give any solution because I already use the Xfermode but I can't have the what I want – Driss Bounouar Apr 04 '13 at 15:05

2 Answers2

1

Instead of trying to draw the Triangle, how about clipping the canvas?

/* set up triangle path... */
path.close();
canvas.save();
canvas.clipPath(path, Region.Op.XOR);
canvas.drawCircle(...);
canvas.restore();
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Thanks for answering @Karakuri but this doesn't show nothing I did my triangle the path.close then what you have written but it gives only a circle! no triangle is showing! – Driss Bounouar Apr 04 '13 at 15:53
0

I think you need to set the layer type to software on Honeycomb and later. Add this at the end of your constructor:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    setLayerType(LAYER_TYPE_SOFTWARE, null);
}
Karakuri
  • 38,365
  • 12
  • 84
  • 104