android 画布区域,android:canvas绘制一个透明矩形并填充其余区域

  • Post author:
  • Post category:其他


我为那些需要它的人共享一个代码,用于为相机绘制SurfaceView的代码,由透明背景上的透明矩形和可选的alpha组成。

欢迎大家有更好的贡献:)

@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);

}

f54o9.jpg