当前位置: 首页 > news >正文

Android 实例 - Android 圆形蒙版(Android 圆形蒙版实现、圆形蒙版解读)

Android 圆形蒙版实现

1、View
public class CircleMaskView extends View {private Paint paint;public CircleMaskView(Context context, AttributeSet attrs) {super(context, attrs);paint = new Paint(Paint.ANTI_ALIAS_FLAG);paint.setColor(Color.TRANSPARENT);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int width = getWidth();int height = getHeight();int centerX = width / 2;int centerY = height / 2;int radius = Math.min(width, height) / 2;canvas.drawCircle(centerX, centerY, radius, paint);}
}
2、Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".fragment.UserFaceIdentifyFragment"tools:ignore="MissingConstraints"><TextViewandroid:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="请面向屏幕上方摄像头"android:textColor="#ff262626"android:textSize="32sp"android:textStyle="bold"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><androidx.constraintlayout.widget.ConstraintLayoutandroid:id="@+id/cl_preview_container"android:layout_width="380dp"android:layout_height="380dp"android:layout_marginTop="60dp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/tv_title"><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="340dp"android:layout_height="340dp"android:orientation="horizontal"android:translationZ="100dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><SurfaceViewandroid:id="@+id/sv_preview"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><Viewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><com.my.smallscreen.view.CircleMaskViewandroid:layout_width="match_parent"android:layout_height="match_parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout></androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

圆形蒙版解读

1、View
(1)类与变量
public class CircleMaskView extends View {...
}
  1. 实现自定义 View,继承 Android 的基础 View 类
private Paint paint;
  1. 定义一个 Paint 对象,即画笔对象,Paint 类可以决定如何绘制图形,例如,颜色、风格、粗细等
(2)构造函数
public CircleMaskView(Context context, AttributeSet attrs) {super(context, attrs);...
}
  1. 调用父类 View 的构造函数,这是标准的用于确保 View 能正确地从 XML 布局属性中初始化的操作
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  1. 创建 Paint 对象,Paint.ANTI_ALIAS_FLAG 是抗锯齿,启用后,图形的边缘会变得更加平滑,不会出现锯齿状
paint.setColor(Color.TRANSPARENT);
  1. 设置画笔的颜色为透明,用这支笔画出的任何形状本身都是透明的
(3)实现蒙版效果
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
  1. setXfermode 方法用于定义当绘制一个新图形(源)到 Canvas 上时,如何与已经存在的图形(目标)进行组合

  2. PorterDuff.Mode.DST_IN 是 PorterDuff 混合模式中的一种,它的规则是,最终结果只保留目标图像中与源图像重叠,且源图像不透明的部分

  3. 简单来说,DST_IN 混合模式的效果就是: 用源图像的 Alpha(透明度)通道来裁剪目标图像

  4. 源图像透明的地方,目标图像就变透明;源图像不透明的地方,目标图像就保持原样

(4)onDraw 方法
@Override
protected void onDraw(Canvas canvas) {super.onDraw(canvas);...
}
  1. 调用父类的绘制方法,但是 View 类中的 onDraw 方法默认是空实现,所以可以省略
int width = getWidth();
int height = getHeight();
int centerX = width / 2;
int centerY = height / 2;
  1. 获取当前 View 的宽度和高度,计算 View 的中心点坐标
int radius = Math.min(width, height) / 2;
  1. 计算圆的半径,取宽和高中的较小值的一半,以确保圆形总能完全显示在 View 中,无论 View 是正方形还是长方形
canvas.drawCircle(centerX, centerY, radius, paint);
  1. 执行绘制,使用之前配置好的 Paint 对象在 Canvas 上绘制一个圆形
2、Layout
<SurfaceViewandroid:id="@+id/sv_preview"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />
  1. 显示摄像头预览画面
<Viewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />
  1. 用一个白色背景覆盖整个预览画面
<com.my.smallscreen.view.CircleMaskViewandroid:layout_width="match_parent"android:layout_height="match_parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />
  1. 使用 CircleMaskView 在白色背景上挖出一个圆形洞,使圆形区域内的摄像头预览画面可见,而圆形区域外显示白色背景
http://www.dtcms.com/a/394854.html

相关文章:

  • PyCharm 在 Linux 上的安装指南
  • Linux 入门:开启开源世界的大门
  • ​​[硬件电路-321]:数字电路的两大家族CMOS(来源于MOS三极管管)与TTL(来源于BJT三极管)
  • 【GitHub每日速递 250922】开源 AI 搜索引擎 Perplexica:本地大模型 + 多模式搜索,免费又强大!
  • CCF-CSP-S 2021 初赛解析
  • 现在如何使用docker下载
  • 【Proteus仿真】AT89C51单片机并行数据转换为串行仿真
  • Docker 容器详解及实操,从新手>入门>拿捏,巨详细
  • 【ASP.NET Core】身份认证——Identity标识框架指南
  • [Nodejs+LangChain+Ollama] 2.提示词实践
  • i.MX6ULL移植内核6.6(二)GPIO子系统和LED子系统
  • VLC Media取消视频文件名前置显示
  • 在unity urp项目中 通过图片创建材质(透明光晕)
  • OneSignal PHP SDK v2 官方资源
  • 如何透过批次模拟加速3D模型建立
  • PDF清晰度提升工具,让模糊文档变清晰
  • 设计模式六大原则
  • QML 多路 RTSP 视频流实时预览实现
  • glTF/glb:现在和未来
  • 构建以ERP为核心的智能制造运营中心(MOM)
  • Java:toArray(new String[0])
  • Trilium Notes+cpolar:打造随身个人知识库的智能中枢
  • 无人机图传技术详解:为何云望图传信号传输能力远超WiFi?,无人机wifi图传是什么意思
  • 水题记录2.1
  • 企业智能工作流的无界解决方案由CherryStudio+cpolar解决
  • Nginx高级用法案例汇总
  • Python开发:使用FastAPI创建后端服务
  • Nginx配置中location和proxy_pass指令尾部是否带斜杠的区别
  • Nginx核心配置
  • 医院不良事件管理系统:提升医疗安全的智能化解决方案