android: canvas draw a transparent rectangle and fill the rest area

Viewed 256

I share a code for those who need it, the code used to draw SurfaceView for camera, consisting of a transparent rectangle on transparent background with optional alpha.

Welcome better contributions from everyone :)

@Override
protected void onDraw(Canvas canvas) {
    mAGPLog.e(this.getClass().getName() + " - onDraw");

    canvas.drawColor(Color.argb(128, 224, 224, 224));

    int margin = (int) getResources().getDimension(R.dimen.margin_camera);
    int width = getWidth();
    int height = getHeight();
    mAGPLog.e("width - " + width + " * height - " + height);

    int distance = width < height ? width - margin : height - margin;
    paint.setColor(Color.CYAN);
    paint.setStrokeWidth(strokerWidth);
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawRect(
            (width - distance) / 2, (height - distance) / 2, (width + distance) / 2, (height + distance) / 2, paint);

    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.argb(0, 255, 255, 255));
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    canvas.drawRect(
            (width - distance) / 2 + strokerWidth, (height - distance) / 2 + strokerWidth, (width + distance) / 2 - strokerWidth, (height + distance) / 2 - strokerWidth, paint);
    super.onDraw(canvas);
}

enter image description here

0 Answers
Related