From a066254a33747b1965e9af4fc068f58adec4a187 Mon Sep 17 00:00:00 2001 From: tamimattafi Date: Fri, 28 Aug 2020 17:38:12 +0700 Subject: [PATCH] Library: Code refactoring --- .../soup/neumorphism/NeumorphShapeDrawable.kt | 23 ++++++--------- .../neumorphism/internal/util/BitmapUtils.kt | 28 ++++++++++--------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/neumorphism/src/main/java/soup/neumorphism/NeumorphShapeDrawable.kt b/neumorphism/src/main/java/soup/neumorphism/NeumorphShapeDrawable.kt index a53da25..22725a5 100644 --- a/neumorphism/src/main/java/soup/neumorphism/NeumorphShapeDrawable.kt +++ b/neumorphism/src/main/java/soup/neumorphism/NeumorphShapeDrawable.kt @@ -13,8 +13,8 @@ import soup.neumorphism.internal.shape.BasinShape import soup.neumorphism.internal.shape.FlatShape import soup.neumorphism.internal.shape.PressedShape import soup.neumorphism.internal.shape.Shape -import soup.neumorphism.internal.util.BitmapUtils.clipToPath import soup.neumorphism.internal.util.BitmapUtils.clipToRadius +import soup.neumorphism.internal.util.BitmapUtils.toBitmap class NeumorphShapeDrawable : Drawable { @@ -312,22 +312,17 @@ class NeumorphShapeDrawable : Drawable { private fun drawBackgroundDrawable(canvas: Canvas) { drawableState.backgroundDrawable?.let { drawable -> - val rect = RectF() outlinePath.computeBounds(rect, true) - val bitmap = drawable.clipToPath(rect) - - val roundBitmap = when (drawableState.shapeAppearanceModel.getCornerFamily()) { - CornerFamily.OVAL -> { - bitmap.clipToRadius(bitmap.height / 2f) - } - else -> { - val cornerSize = drawableState.shapeAppearanceModel.getCornerSize() - bitmap.clipToRadius(cornerSize) - } - } - canvas.drawBitmap(roundBitmap, rect.left, rect.top, null) + val rectWidth = rect.width().toInt() + val rectHeight = rect.height().toInt() + val bitmap = drawable.toBitmap(rectWidth, rectHeight) + + val cornerSize = if (drawableState.shapeAppearanceModel.getCornerFamily() == CornerFamily.OVAL) bitmap.height / 2f + else drawableState.shapeAppearanceModel.getCornerSize() + + canvas.drawBitmap(bitmap.clipToRadius(cornerSize), rect.left, rect.top, null) } } diff --git a/neumorphism/src/main/java/soup/neumorphism/internal/util/BitmapUtils.kt b/neumorphism/src/main/java/soup/neumorphism/internal/util/BitmapUtils.kt index 5870395..82e6798 100644 --- a/neumorphism/src/main/java/soup/neumorphism/internal/util/BitmapUtils.kt +++ b/neumorphism/src/main/java/soup/neumorphism/internal/util/BitmapUtils.kt @@ -3,27 +3,29 @@ package soup.neumorphism.internal.util import android.graphics.* import android.graphics.drawable.Drawable + object BitmapUtils { - fun Drawable.clipToPath(rect: RectF): Bitmap { - val output = Bitmap.createBitmap(rect.width().toInt(), rect.height().toInt(), Bitmap.Config.ARGB_8888) - val canvas = Canvas(output) - this.setBounds(0, 0, canvas.width, canvas.height) - this.draw(canvas) + fun Drawable.toBitmap(width: Int, height: Int): Bitmap { + setBounds(0, 0, width, height) + val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) + Canvas(output).let(::draw) return output } fun Bitmap.clipToRadius(radius: Float): Bitmap { - val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) - val canvas = Canvas(output) - val paint = Paint() + val paint = Paint(Paint.ANTI_ALIAS_FLAG) val rect = Rect(0, 0, width, height) val rectF = RectF(rect) - paint.isAntiAlias = true - canvas.drawARGB(0, 0, 0, 0) - canvas.drawRoundRect(rectF, radius, radius, paint) - paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) - canvas.drawBitmap(this, rect, rect, paint) + + val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) + Canvas(output).apply { + drawARGB(0, 0, 0, 0) + drawRoundRect(rectF, radius, radius, paint) + paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN) + drawBitmap(this@clipToRadius, rect, rect, paint) + } + return output }