问题描述
如何在 android/kotlin 上为 bottomappbar 模糊背景 我将背景颜色设置为透明,但我希望它变得模糊
解决方法
目前我使用 blurry 实现如下,但直到缓慢我找到了好方法,但它也可以帮助您进一步研究。
请注意我添加了自定义底栏
Bellow 是一些实用程序代码,可能对您有帮助
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,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,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,this.width,this.height)
paint.setAntiAlias(true)
canvas.drawARGB(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,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
}
}