如何从 android 中的根视图移除模糊
How to remove the blur from root view in android
我正在使用这个 class 来模糊我 activity 中根视图的背景:
object BlurBuilder {
private val BITMAP_SCALE = 0.4f
private val BLUR_RADIUS = 20f
fun blur(v: View): Bitmap {
return calculateBlur(v.context, getScreenshot(v))
}
fun calculateBlur(ctx: Context, image: Bitmap): Bitmap {
val width = Math.round(image.width * BITMAP_SCALE)
val height = Math.round(image.height * BITMAP_SCALE)
val inputBitmap = Bitmap.createScaledBitmap(image, width, height, false)
val outputBitmap = Bitmap.createBitmap(inputBitmap)
val rs = RenderScript.create(ctx)
val theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))
val tmpIn = Allocation.createFromBitmap(rs, inputBitmap)
val tmpOut = Allocation.createFromBitmap(rs, outputBitmap)
theIntrinsic.setRadius(BLUR_RADIUS)
theIntrinsic.setInput(tmpIn)
theIntrinsic.forEach(tmpOut)
tmpOut.copyTo(outputBitmap)
return outputBitmap
}
fun getScreenshot(v: View): Bitmap {
val b = Bitmap.createBitmap(v.width, v.height, Bitmap.Config.ARGB_8888)
val c = Canvas(b)
v.draw(c)
return b
}
}
在我的 activity 中,我有以下内容:
fun applyBlur() {
val view = this.findViewById(android.R.id.content).rootView
if (view.width > 0) {
val image = BlurBuilder.blur(view)
window.setBackgroundDrawable(BitmapDrawable(this.resources, image))
} else {
view.viewTreeObserver.addOnGlobalLayoutListener({
val image = BlurBuilder.blur(view)
window.setBackgroundDrawable(BitmapDrawable(this.resources, image))
})
}
}
通过这种技术,我根据模糊半径模糊了 activity 的根视图。我该怎么做呢?我试图将 BLUR_RADIUS 设置为 0.1f 但它仍然不起作用。
请提供一些关于如何实现此目的的解释。谢谢!
模糊是一种破坏性的操作,反转它需要一些复杂的数学和计算,你最好添加一个标志,并在模糊函数中检查它,如果标志为假,例如 -- 只需传递原始背景通过,例如:
var contentBG: Drawable? = null
var needBlur = true
fun applyBlur() {
val view = this.findViewById(android.R.id.content).rootView
if (view.width > 0) {
contentBG ?: let { contentBG = view.background }
val drawable = if (needBlur)
BitmapDrawable(this.resources, BlurBuilder.blur(view))
else contentBG
window.setBackgroundDrawable(drawable)
} else {
view.viewTreeObserver.addOnGlobalLayoutListener({
val image = BlurBuilder.blur(view)
window.setBackgroundDrawable(BitmapDrawable(this.resources, image))
})
}
}
我正在使用这个 class 来模糊我 activity 中根视图的背景:
object BlurBuilder {
private val BITMAP_SCALE = 0.4f
private val BLUR_RADIUS = 20f
fun blur(v: View): Bitmap {
return calculateBlur(v.context, getScreenshot(v))
}
fun calculateBlur(ctx: Context, image: Bitmap): Bitmap {
val width = Math.round(image.width * BITMAP_SCALE)
val height = Math.round(image.height * BITMAP_SCALE)
val inputBitmap = Bitmap.createScaledBitmap(image, width, height, false)
val outputBitmap = Bitmap.createBitmap(inputBitmap)
val rs = RenderScript.create(ctx)
val theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))
val tmpIn = Allocation.createFromBitmap(rs, inputBitmap)
val tmpOut = Allocation.createFromBitmap(rs, outputBitmap)
theIntrinsic.setRadius(BLUR_RADIUS)
theIntrinsic.setInput(tmpIn)
theIntrinsic.forEach(tmpOut)
tmpOut.copyTo(outputBitmap)
return outputBitmap
}
fun getScreenshot(v: View): Bitmap {
val b = Bitmap.createBitmap(v.width, v.height, Bitmap.Config.ARGB_8888)
val c = Canvas(b)
v.draw(c)
return b
}
}
在我的 activity 中,我有以下内容:
fun applyBlur() {
val view = this.findViewById(android.R.id.content).rootView
if (view.width > 0) {
val image = BlurBuilder.blur(view)
window.setBackgroundDrawable(BitmapDrawable(this.resources, image))
} else {
view.viewTreeObserver.addOnGlobalLayoutListener({
val image = BlurBuilder.blur(view)
window.setBackgroundDrawable(BitmapDrawable(this.resources, image))
})
}
}
通过这种技术,我根据模糊半径模糊了 activity 的根视图。我该怎么做呢?我试图将 BLUR_RADIUS 设置为 0.1f 但它仍然不起作用。
请提供一些关于如何实现此目的的解释。谢谢!
模糊是一种破坏性的操作,反转它需要一些复杂的数学和计算,你最好添加一个标志,并在模糊函数中检查它,如果标志为假,例如 -- 只需传递原始背景通过,例如:
var contentBG: Drawable? = null
var needBlur = true
fun applyBlur() {
val view = this.findViewById(android.R.id.content).rootView
if (view.width > 0) {
contentBG ?: let { contentBG = view.background }
val drawable = if (needBlur)
BitmapDrawable(this.resources, BlurBuilder.blur(view))
else contentBG
window.setBackgroundDrawable(drawable)
} else {
view.viewTreeObserver.addOnGlobalLayoutListener({
val image = BlurBuilder.blur(view)
window.setBackgroundDrawable(BitmapDrawable(this.resources, image))
})
}
}