1

I'd like to implement custom ViewGroup which is displaying single View inside. The tricky part is that the child view have to be crop into circle shape and it have to be drawn fast.

What is the best way to implement this behavior?

My current implementation looks like this:

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    child.setDrawingCacheEnabled(true);
    child.buildDrawingCache();
    child.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    canvas.drawBitmap(getCroppedBitmap(child.getDrawingCache()), outerWidth, outerWidth, new Paint());
    child.setLayerType(View.LAYER_TYPE_NONE, null);
    return true;
}

public Bitmap getCroppedBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    childPaint.setXfermode(null);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth() / 2, childPaint);
    childPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, mBoundsI, mBoundsI, childPaint);
    return output;
}

(Every time new Canvas and Bitmap is created, and drawing cache is enabled)

VizGhar
  • 3,036
  • 1
  • 25
  • 38

1 Answers1

0

probably the fastest way of achieving that is to let the views draw directly on canvas (avoiding the buffer and bitmap creation).

For that you should specify on the canvas a clip path and with a Path that is set to an appropriate circle.

Then the Canvas will not draw anything that is draw outside it's clipping path

ps.: remember to use the appropriate REPLACE Region on subsequent calls on the clipPath

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Perfect! I have placed initialization of clip path into constructor, set clip path in onMeasure and in drawChild() called canvas.clipPath(clipPath, Region.Op.REPLACE); super.drawChild(canvas, child, drawingTime); works well – VizGhar Jun 04 '14 at 15:14
  • happy I could help. I never used this methods, but always wandered how easy is to make them work. – Budius Jun 04 '14 at 15:16
  • 2
    and also setLayerType(View.LAYER_TYPE_SOFTWARE, null); was needed – VizGhar Jun 04 '14 at 15:57
  • i never used a canvas, and i cant understand what are you talking about even !!! cant you give a sample code !? – Ahmed Adel Ismail Aug 11 '15 at 11:56
  • Please see this site http://stackoverflow.com/tour > "Get answers to practical, detailed questions" > "Don't ask about..." > "Questions you haven't tried to find an answer for (show your work!)" – VizGhar Aug 12 '15 at 13:07