本文主要是介绍android中给view添加遮罩层,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.在 res 目录中添加 id :
<?xml version="1.0" encoding="utf-8"?>
<resources><item name="view_mask_id" type="id" />
</resources>
2.扩展方法:
/** 给一个 View 添加一层由 [res] 填充的遮罩层布局, 可通过 [isShowOrigin] 控制是否显示原始布局, 返回 [res] 填充后的布局. */
fun View?.addMask(@LayoutRes res: Int, isShowOrigin: Boolean = true): View? {this ?: run {Logs.w("addMask error! -----> view is null!")return null}val maskLayout = LayoutInflater.from(this.context).inflate(res, null, false)addMask(maskLayout, isShowOrigin)return maskLayout
}/** 给一个 View 添加一层 [maskLayout] 遮罩层布局, 可通过 [isShowOrigin] 控制是否显示原始布局. */
fun View?.addMask(maskLayout: View, isShowOrigin: Boolean = true) {this ?: run {Logs.w("addMask error! -----> view is null!")return}val parent = this.parent as? ViewGroupparent ?: run {Logs.w("addMask error! -----> parent is null!")return}val originLayout = thisval maskLayoutContainer = FrameLayout(originLayout.context)// 将原始布局替换为 container.originLayout.replaceWith(maskLayoutContainer)maskLayoutContainer.apply {val lp = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)// 如果需要显示原始布局, 添加原始布局到 container 中.if (isShowOrigin) {maskLayoutContainer.addView(originLayout.apply { layoutParams = lp })}// 添加 mask 布局.maskLayoutContainer.addView(maskLayout.apply { layoutParams = lp })}originLayout.setTag(R.id.view_mask_id, maskLayoutContainer)
}/*** 移除遮罩层布局.*/
fun View?.removeMask() {this ?: let {Logs.w("removeMask error! -----> View is null!")return}val originLayout = thisval maskLayoutContainer = this.getTag(R.id.view_mask_id) as? ViewGroupmaskLayoutContainer ?: let {Logs.w("removeMask error! -----> maskLayout is null!")return}maskLayoutContainer.replaceWith(originLayout)
}/** 将 View 替换为 [res] 填充后的布局, 返回 [res] 填充后的布局. */
fun View?.replaceWith(@LayoutRes res: Int): View? {this ?: run {Logs.w("replaceWith error! -----> view is null!")return null}val replaceLayout = LayoutInflater.from(this.context).inflate(res, null, false)replaceWith(replaceLayout)return replaceLayout
}/** 将 View 替换为 [replaceView]. */
fun View?.replaceWith(replaceView: View) {this ?: run {Logs.w("replaceWith error! -----> view is null!")return}val parent = this.parent as? ViewGroupparent ?: run {Logs.w("replaceWith error! -----> parent is null!")return}val originView = thisval originViewIndex = parent.children.indexOf(originView)if (originViewIndex >= 0) {replaceView.id = originView.idparent.removeViewAt(originViewIndex) // 移除原始布局.replaceView.removeFromParent() // 添加之前, 先从父布局移除 replaceView.parent.addView(replaceView, originViewIndex, originView.layoutParams)} else {Logs.w("replaceWith error! -----> child index is $originViewIndex")}
}/** 将 View 从它的父布局移除. */
fun View?.removeFromParent() {this ?: run {Logs.w("removeFromParent error! -----> view is null!")return}val parent = this.parent as? ViewGroupparent ?: run {Logs.w("removeFromParent error! -----> parent is null!")return}parent.removeView(this)
}
这篇关于android中给view添加遮罩层的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!