Android 獲取 View 的高斯模糊 Drawable

Android 獲取 View 的高斯模糊 Drawable

使用谷歌接口,不兼容 17 以下,需要兼容需處理

object RenderScriptUtil {
    const val RADIUS = 25F //高斯模糊程度 0~25
    /**
     * 獲取 View 的高斯模糊 BitmapDrawable
     */
    fun gaussianBlur(v: View): BitmapDrawable {
        val renderScript = RenderScript.create(v.context)
        v.setDrawingCacheEnabled(true)
        val origin = v.getDrawingCache()
        val input = Allocation.createFromBitmap(renderScript, origin)
        val output = Allocation.createTyped(renderScript, input.type)
        val scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript))
        scriptIntrinsicBlur.setRadius(RADIUS)
        scriptIntrinsicBlur.setInput(input)
        scriptIntrinsicBlur.forEach(output)
        output.copyTo(origin)
        val bg = Bitmap.createBitmap(origin)
        v.setDrawingCacheEnabled(false)
        return BitmapDrawable(bg)
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章