Improved ImageRenderer Kotlin code
@@ -12,8 +12,7 @@ import com.facebook.fresco.vito.renderer.util.ColorUtils
typealias RenderCommand = (Canvas) -> Unit
-class ImageRenderer {
- companion object {
+object ImageRenderer {
/**
* Creates an image data model render command.
@@ -38,28 +37,17 @@ class ImageRenderer {
shape: Shape,
paint: Paint,
imageTransformation: Matrix? = null
- ): RenderCommand {
+ ): RenderCommand =
when (shape) {
- is RectShape -> {
- return {
- it.concat(imageTransformation)
- it.drawBitmap(bitmap, 0f, 0f, paint)
- }
- }
+ is RectShape -> bitmapRenderCommand(paint, bitmap, imageTransformation)
is CircleShape ->
if (!isBitmapCircular) {
- val paint = getBitmapPaint(bitmap, paint, imageTransformation)
- return { shape.draw(it, paint) }
+ paintRenderCommand(shape, paint.setBitmap(bitmap, imageTransformation))
} else {
- return {
- it.concat(imageTransformation)
- it.drawBitmap(bitmap, 0f, 0f, paint)
- }
+ bitmapRenderCommand(paint, bitmap, imageTransformation)
}
else -> {
- val paint = getBitmapPaint(bitmap, paint, imageTransformation)
- return { shape.draw(it, paint) }
- }
+ paintRenderCommand(shape, paint.setBitmap(bitmap, imageTransformation))
}
}
@@ -69,8 +57,8 @@ class ImageRenderer {
imageTransformation: Matrix? = null
): RenderCommand {
// The image transformation is a no-op for solid colors since it is a no-op
- val paint = paint.apply { color = ColorUtils.multiplyColorAlpha(colorInt, paint.alpha) }
- return { shape.draw(it, paint) }
+ paint.color = ColorUtils.multiplyColorAlpha(colorInt, paint.alpha)
+ return paintRenderCommand(shape, paint)
}
inline fun DrawableImageDataModel.createRenderCommand(
@@ -82,10 +70,10 @@ class ImageRenderer {
// We transform by scaling the Canvas, so we let the Drawable draw itself with its
// preferred dimensions
when (shape) {
- is RectShape -> return {
+ is RectShape -> return { canvas ->
if (width >= 0 && height >= 0) {
drawable.setBounds(0, 0, width, height)
- it.concat(imageTransformation)
+ canvas.concat(imageTransformation)
} else {
// The image dimensions are not set, so we assume the image can stretch to fit the
// entire rect, so we do not apply the transformation.
@@ -97,29 +85,40 @@ class ImageRenderer {
}
drawable.colorFilter = colorFilter
drawable.alpha = paint.alpha
- drawable.draw(it)
+ drawable.draw(canvas)
}
else -> {
- return {
+ return { canvas ->
drawable.setBounds(0, 0, width, height)
drawable.colorFilter = null // The Paint handles the color filter
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
drawable.draw(Canvas(bitmap))
- shape.draw(it, getBitmapPaint(bitmap, paint, imageTransformation))
+ paint.setBitmap(bitmap, imageTransformation)
+ shape.draw(canvas, paint)
}
}
}
}
- inline fun getBitmapPaint(
- bitmap: Bitmap,
+ inline fun bitmapRenderCommand(
paint: Paint,
+ bitmap: Bitmap,
+ imageTransformation: Matrix?
+ ): RenderCommand = { canvas ->
+ canvas.concat(imageTransformation)
+ canvas.drawBitmap(bitmap, 0f, 0f, paint)
+ }
+
+ inline fun paintRenderCommand(shape: Shape, paint: Paint): RenderCommand = { canvas ->
+ shape.draw(canvas, paint)
+ }
+
+ inline fun Paint.setBitmap(
+ bitmap: Bitmap,
shaderTransformation: Matrix? = null,
): Paint {
- return paint.apply {
shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
shader.setLocalMatrix(shaderTransformation)
- }
- }
+ return this
}
}
