我如何改进这些功能以在 recyclerView 上绑定位图?
how can i improve these functions to bind bitmaps on recyclerView?
我有两个用于将图像绑定到 recyclerview 的函数,一个用于将字符串 (base64) 转换为位图,另一个函数用于圆化所述图像的角。
//convert string to bitmap
fun stringToBitMap( encodedString: String): Bitmap? {
println("string to bitmap is being called")
return try {
val encodeByte: ByteArray = Base64.decode(encodedString, Base64.DEFAULT)
BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.size)
} catch (e: Exception) {
println("Failed to convert string to bitmap")
e.message
null
}
}
//round corners
fun getRoundedCornerBitmap(bitmap: Bitmap, pixels: Int): Bitmap {
println("get rounded corners is being called")
val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)
val color = -0xbdbdbe
val paint = Paint()
val rect = Rect(0, 0, bitmap.width, bitmap.height)
val rectF = RectF(rect)
val roundPx = pixels.toFloat()
paint.isAntiAlias = true
canvas.drawARGB(0, 0, 0, 0)
paint.color = color
canvas.drawRoundRect(rectF, roundPx, roundPx, paint)
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(bitmap, rect, rect, paint)
return output
}
然后我用 BindingAdapter 注释我的最终函数,然后我从 xml 文件中调用该函数
@BindingAdapter("poster")
fun image (view: ImageView, image: String) {
return view.setImageBitmap(stringToBitMap(image)?.let { getRoundedCornerBitmap(it, 10) })
}
它可以工作,但在某些设备上性能很差,我在低资源 phone(三星 SM-J106B)中调试我的应用程序并且 cpu 使用率的峰值是 35%快速滚动(我的图像不是高分辨率,只有 400x400),而且 recyclerview 不断调用这些函数,这使得滚动有点缓慢。那么问题来了,我该如何改进我的功能?
pd: 我是个新手:(
我最终使用了这样的滑行:
fun poster(view: ImageView, image: String) {
val imageByteArray: ByteArray = Base64.decode(image, Base64.DEFAULT)
val round = RequestOptions
.bitmapTransform(RoundedCorners(14))
Glide.with(view)
.load(imageByteArray)
.apply(round)
.into(view)
}
现在性能更好了:D
我有两个用于将图像绑定到 recyclerview 的函数,一个用于将字符串 (base64) 转换为位图,另一个函数用于圆化所述图像的角。
//convert string to bitmap
fun stringToBitMap( encodedString: String): Bitmap? {
println("string to bitmap is being called")
return try {
val encodeByte: ByteArray = Base64.decode(encodedString, Base64.DEFAULT)
BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.size)
} catch (e: Exception) {
println("Failed to convert string to bitmap")
e.message
null
}
}
//round corners
fun getRoundedCornerBitmap(bitmap: Bitmap, pixels: Int): Bitmap {
println("get rounded corners is being called")
val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)
val color = -0xbdbdbe
val paint = Paint()
val rect = Rect(0, 0, bitmap.width, bitmap.height)
val rectF = RectF(rect)
val roundPx = pixels.toFloat()
paint.isAntiAlias = true
canvas.drawARGB(0, 0, 0, 0)
paint.color = color
canvas.drawRoundRect(rectF, roundPx, roundPx, paint)
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
canvas.drawBitmap(bitmap, rect, rect, paint)
return output
}
然后我用 BindingAdapter 注释我的最终函数,然后我从 xml 文件中调用该函数
@BindingAdapter("poster")
fun image (view: ImageView, image: String) {
return view.setImageBitmap(stringToBitMap(image)?.let { getRoundedCornerBitmap(it, 10) })
}
它可以工作,但在某些设备上性能很差,我在低资源 phone(三星 SM-J106B)中调试我的应用程序并且 cpu 使用率的峰值是 35%快速滚动(我的图像不是高分辨率,只有 400x400),而且 recyclerview 不断调用这些函数,这使得滚动有点缓慢。那么问题来了,我该如何改进我的功能?
pd: 我是个新手:(
我最终使用了这样的滑行:
fun poster(view: ImageView, image: String) {
val imageByteArray: ByteArray = Base64.decode(image, Base64.DEFAULT)
val round = RequestOptions
.bitmapTransform(RoundedCorners(14))
Glide.with(view)
.load(imageByteArray)
.apply(round)
.into(view)
}
现在性能更好了:D