模糊约束布局 api 在 25 和 31 之间 - android

问题描述 投票:0回答:1

模糊背景未正确应用。

**applyBlurEffect(wholeWrapper)**

private fun applyBlurEffect(view: View) {
    view.viewTreeObserver.addOnPreDrawListener(
            object : ViewTreeObserver.OnPreDrawListener {
                override fun onPreDraw(): Boolean {
                    view.viewTreeObserver.removeOnPreDrawListener(this)

                    Log.d("BlurEffect", "View width: ${view.width}, height: ${view.height}")
                    if (view.width <= 0 || view.height <= 0) {
                        Log.e("BlurEffect", "Invalid view dimensions")
                        return true
                    }

                    // Capture and blur the view
                    val bitmap = BlurBuilder.getScreenshot(view)
                    val blurredBitmap = BlurBuilder.blur(view.context, bitmap)
                    view.background = BitmapDrawable(view.resources, blurredBitmap)
                    Log.d("BlurEffect", "onPreDraw:  bitmap $bitmap blurredBitmap $blurredBitmap")
                    return true
                }
            }
    )
}
object BlurBuilder {
    private const val BITMAP_SCALE = 0.4f
    private const val BLUR_RADIUS = 7.5f

    fun blur(v: View): Bitmap {
        return blur(v.context, getScreenshot(v))
    }

    fun blur(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
    }
}

应用时,当宽度和高度不为 0 时,背景应该模糊。因为我正在从 api 获取数据来绘制图表。整个包装器是 ConstraintLayout。我检查了宽度和高度不为零+创建了位图和模糊位图,但没有任何内容是模糊的。

android xml android-layout view blur
1个回答
0
投票

只需搜索chatgpt bro xd,它就会在几秒钟内解决你的问题

© www.soinside.com 2019 - 2024. All rights reserved.