美文网首页Android
Android背景高斯模糊工具类

Android背景高斯模糊工具类

作者: 在一颗大大大榕树下 | 来源:发表于2019-02-15 15:37 被阅读0次

kotlin高斯模糊工具类:
使用方法:

BlurHelper().setContainerView(ll_root)
                .setImageResourse(R.mipmap.login_bg)
                .setContext(this.applicationContext)
                .build()

工具类:

class BlurHelper(){
    private var mImageId:Int? = null//背景图
    private var mContainer:View? = null//容器
    private var mContext: Context? = null//上下文
    private var mRadius: Float = 10f//模糊程度

    fun setImageResourse(id:Int): BlurHelper {
        mImageId = id
        return this
    }
    fun setContainerView(view:View): BlurHelper {
        mContainer = view
        return this
    }

    fun setRadius(radius:Float): BlurHelper {
        mRadius = radius
        return this
    }
    fun setContext(context: Context): BlurHelper {
        mContext = context
        return this
    }

    private fun applyBlur() {
        //默认状态下是以桌面壁纸为背景
        var bmp:Bitmap = if(mImageId==null&& mImageId !is Int){
            Log.i("GasBlurHelperConfig","mImageId is noconfig,use the wallpaper!")
            val wallpaperManager = WallpaperManager.getInstance(mContext)
            val wallpaperDrawable = wallpaperManager.drawable
            (wallpaperDrawable as BitmapDrawable).bitmap
        }else{
            BitmapFactory.decodeResource(mContext!!.resources,mImageId!!)
        }
        blur(bmp)
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private fun blur(bkg: Bitmap) {
        var bkg = bkg
        val startMs = System.currentTimeMillis()
        var radius:Float = if(mRadius<25f&&mRadius>0f){
            mRadius
        }else{
            10f
        }
        bkg = small(bkg)
        var bitmap = bkg.copy(bkg.config, true)

        val rs = RenderScript.create(mContext)
        val input = Allocation.createFromBitmap(rs, bkg, Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT)
        val output = Allocation.createTyped(rs, input.type)
        val script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))
        script.setRadius(radius)
        script.setInput(input)
        script.forEach(output)
        output.copyTo(bitmap)

        bitmap = big(bitmap)
        setBackground(mContainer!!,BitmapDrawable(mContext!!.resources, bitmap))
        rs.destroy()
        Log.d("BlurHelper", "blur take away:" + (System.currentTimeMillis() - startMs) + "ms")
    }

    private fun big(bitmap: Bitmap): Bitmap {
        val matrix = Matrix()
        matrix.postScale(2f, 2f) //长和宽放大缩小的比例
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
    }

    private fun small(bitmap: Bitmap): Bitmap {
        val matrix = Matrix()
        matrix.postScale(0.25f, 0.25f) //长和宽放大缩小的比例
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
    }

    fun build() {
        if (mContext == null) {
            Log.e("GasBlurHelperConfig", "contextConfig is null!")
            return
        }

        if (mContainer == null && mContainer !is View) {
            Log.e("GasBlurHelperConfig", "mContainerId is null!")
            return
        }
        applyBlur()
    }
}

相关文章

网友评论

    本文标题:Android背景高斯模糊工具类

    本文链接:https://www.haomeiwen.com/subject/ccfmeqtx.html