0

Okay, have to look to the graphics experts on this one. I have a rectangular bitmap (large) and a circular bitmap (small). I want to place the large rectangular bitmap behind the smaller circular bitmap with the larger rectangular bitmap taking on the shape of the smaller circular bitmap (i.e. the rectangular bitmap should be clipped or otherwise). The smaller circular bitmap has some transparency and so the clipped rectangular bitmap will show through after transformation. I have tried the following:

protected void onDraw(Canvas c) {

int sc = canvas.saveLayer(x, y, x + w, y + h, 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.drawBitmap(bmpSmallCircular, 0, 0, backgroundPaint);
backgroundPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
canvas.drawBitmap(bmpLargeRectangular, 0 , 0, backgroundPaint);
backgroundPaint.setXfermode(null);
canvas.restoreToCount(sc);

}

So, this gives part of the solution but the rectangular bitmap draws over the smaller bitmap (though it does assume the shape of the smaller). I am probably missing something obvious and would appreciate a point or push in the right direction. Thanks!

1 Answers1

0

first draw the large bitmap then draw the small bitmap you need to swap the statement just

canvas.drawBitmap(bmpLargeRectangular, 0 , 0, backgroundPaint);
canvas.drawBitmap(bmpSmallCircular, 0, 0, backgroundPaint);
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • Thanks, already tried that. Since the rectangle is larger than the circle, it extends beyond the circle (not what I need). I need the circle to cover the rectangle with any portion of the rectangle outside the circle to be hidden. – user1312428 Apr 04 '12 at 13:33
  • can you show the image how actually you want and how you currently getting – Pratik Apr 04 '12 at 13:40
  • Tried to add image but since I am new, looks like that's an issue. However, I did upload sample to here: http://img805.imageshack.us/img805/7628/circleoverlay.png Hope that makes it through. If you can view the image, to the right of the arrow, the hashed area of the rectangle is hidden from view. Thanks. – user1312428 Apr 04 '12 at 14:27