微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决android:canvas绘制一个透明矩形并填充其余区域

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

欢迎大家做出更好的贡献:)

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

    canvas.drawColor(Color.argb(128,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));
    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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。