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)
}
}
来源:CSDN
作者:qinxue24
链接:https://blog.csdn.net/qinxue24/article/details/103629591