【问题标题】:How can I blur background for bottomappbar ?android/kotlin如何模糊bottomappbar的背景?android/kotlin
【发布时间】:2021-09-17 23:24:15
【问题描述】:

如何在 android/kotlin 上为 bottomappbar 模糊背景 我让背景颜色透明,但我希望它是模糊的

like this

【问题讨论】:

  • 请添加您的代码,以完成问题。

标签: android kotlin blur bottomappbar


【解决方案1】:

目前我使用blurry 实现如下所示,但直到慢慢我才找到好方法,但它也有助于您进一步研究。

请注意我添加了自定义底栏

下面是一些可能对你有帮助的实用代码

object BlurUtil {

    fun ImageView.performBlurView(
        activity: Activity,
        viewFrom: View,
        radius: Int,
        sampling: Int,
        colorCode: String,
        isShapedCut: Boolean
    ): Bitmap? {
        if (colorCode.isEmpty()) {
            val bitmap = Blurry.with(activity)
                .radius(radius)
                .sampling(sampling)
                .capture(viewFrom).get()
            val newBitmap = bitmap.getBitmapFromMain(this, isShapedCut)
            this.setImageDrawable(
                BitmapDrawable(
                    resources,
                    newBitmap
                )
            )
            return newBitmap
        } else {
            val bitmap = Blurry.with(activity)
                .radius(radius)
                .sampling(1)
                .color(Color.parseColor(colorCode))
                .capture(viewFrom).get()
            val newBitmap = bitmap.getBitmapFromMain(this, isShapedCut)
            this.setImageDrawable(
                BitmapDrawable(
                    resources,
                    newBitmap
                )
            )
            return newBitmap
        }
    }


    private fun Bitmap.getBitmapFromMain(
        viewTo: View,
        isShapedCut: Boolean
    ): Bitmap? {
        try {
            val x = returnCordinateScreen(viewTo)[0];

            val y = returnCordinateScreen(viewTo)[1];
            val width = viewTo.measuredWidth;
            var height = viewTo.measuredHeight;

            if ((y + height) > this.height) {
                height = (this.height) - (y)
            }
            return Bitmap.createBitmap(
                this,
                x,
                y,
                width,
                height
            )
            Timber.d("x coordinate1 =  %s ", x)
            Timber.d("y coordinate1 =  %s ", y)
            Timber.d("width viewTo1 =  %s ", width)
            Timber.d("height viewTo1 =  %s ", height)
            Timber.d("height bitmap1 =  %s ", this.height)
            Timber.d("width bitmap1 =  %s ", this.width)
        } catch (e: Exception) {
            e.printStackTrace()
            val x = returnCordinateWindow(viewTo)[0];
            val y = returnCordinateWindow(viewTo)[1];
            val width = viewTo.measuredWidth;
            var height = viewTo.measuredHeight;

            if ((y + height) > this.height) {
                height = this.height - y
            }

            return Bitmap.createBitmap(
                this,
                x,
                y,
                width,
                height
            )
            Timber.d("x coordinate2 =  %s ", x)
            Timber.d("y coordinate2 =  %s ", y)
            Timber.d("width viewTo2 =  %s ", width)
            Timber.d("height viewTo2 =  %s ", height)
            Timber.d("height bitmap2 =  %s ", this.height)
            Timber.d("width bitmap2 =  %s ", this.width)
        }
    }

    private fun returnCordinateScreen(viewFrom: View): IntArray {
        val locationFrom = IntArray(2)
        viewFrom.getLocationOnScreen(locationFrom)
        return locationFrom
    }

    private fun returnCordinateWindow(viewFrom: View): IntArray {
        val locationFrom = IntArray(2)
        viewFrom.getLocationInWindow(locationFrom)
        return locationFrom
    }


    private fun Bitmap.getCroppedBitmap(): Bitmap? {
        val output = Bitmap.createBitmap(
            this.width,
            this.height, Bitmap.Config.ARGB_8888
        )
        val canvas = Canvas(output)
        val color = -0xbdbdbe
        val paint = Paint()
        val rect = Rect(0, 0, this.width, this.height)
        paint.setAntiAlias(true)
        canvas.drawARGB(0, 0, 0, 0)
        paint.setColor(color)
        canvas.drawCircle(
            (this.width / 2).toFloat(), (this.height / 2).toFloat(),
            (this.width / 2).toFloat(), paint
        )
        paint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.SRC_IN))
        canvas.drawBitmap(this, rect, rect, paint)
        return output
    }

    private fun Bitmap.getCropBitmap(viewTo: View): Bitmap {
        return Bitmap.createBitmap(
            this,
            viewTo.coordinateOfView()[0],
            viewTo.coordinateOfView()[1],
            viewTo.measuredWidth,
            viewTo.measuredHeight
        )
    }

    private fun View.coordinateOfView(): IntArray {
        val locationFrom = IntArray(2)
        this.getLocationOnScreen(locationFrom)
        return locationFrom
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 2015-10-26
    • 1970-01-01
    • 2018-04-24
    • 2013-10-19
    • 2021-05-04
    相关资源
    最近更新 更多