# android 挖空效果,从PopupWindow中引导图挖空 站点:答源(https://www.dandroid.cn/) 原文:https://www.dandroid.cn/archives/21458 Canonical:https://www.dandroid.cn/archives/21458 作者:admin 发布日期:2023-11-14 最后更新:2023-11-14 10:00 分类:Android应用源码 / 弹出视图 内容类型:post 说明:本页是原文的纯文本镜像,供大模型与检索器完整读取;不含导航、广告、付费框。引用请以上方「原文」链接为准。 ## 正文 自定义AreaMaskView class AreaMaskView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null ): View(context, attrs) { /** * 目标布局周围的空白区域 */ private val brimRect = RectF() /** * 显示目标区域的画笔 */ private val brimPaint = Paint() /** * 圆角 */ private var mRadius = dpToPx(8f) /** * 蒙版的背景颜色 */ private val bgColor = Color.parseColor("#80222427") init { brimPaint.color = Color.TRANSPARENT brimPaint.maskFilter = BlurMaskFilter(10f,BlurMaskFilter.Blur.INNER) //图像合成模式为CLEA brimPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR) setLayerType(LAYER_TYPE_SOFTWARE, null) } fun setTargetRect(targetRect:Rect){ brimRect.left = targetRect.left.toFloat() brimRect.right = targetRect.right.toFloat() val top = targetRect.top.toFloat() - getStatusBarHeight() brimRect.top = top brimRect.bottom = targetRect.bottom.toFloat() - getStatusBarHeight() invalidate() } override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) canvas?.drawColor(bgColor) canvas?.drawRoundRect(brimRect, mRadius, mRadius, brimPaint) canvas?.save() canvas?.restore() } } XxxxPopupWindow弹框 class XxxxPopupWindow( val context: Context, val callback: ()->Unit ) : XxxxPopupWindow(context) { /** * 当前蒙版的目标布局的坐标位置 */ private val targetRect = Rect() fun showAtLocation(anchor: View) { anchor.getGlobalVisibleRect(targetRect) mBinding.AreaMaskView(布局中的id).setTargetRect(targetRect) } } --- 由 答源 DAN 底座 1.4.1 生成 · 2026-11-24T14:13:51+00:00