1

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

像这样

4

1 回答 1

0

目前我使用模糊实现如下所示,但直到缓慢我才找到好方法,但它也可以帮助您进行进一步的研究。

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

在此处输入图像描述

贝娄是一些可能对您有所帮助的实用程序代码

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
    }
}
于 2021-07-07T12:15:41.960 回答