How can I blur background for bottomappbar ?android/kotlin

593 views Asked by At

How can I blur background for bottomappbar on android/kotlin I made the background color transparent, but I want it to be blurry

like this

1

There are 1 answers

0
Dhaval Solanki On

Currently I implemented like below using blurry but till slow I am finding good way but it also help you for further research.

Plz note I added custom bottombar

enter image description here

Bellow is some utility code which might be help you

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
    }
}