Skip to content

Commit

Permalink
Library: Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tamimattafi committed Aug 28, 2020
1 parent ca9c881 commit a066254
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit a066254

Please sign in to comment.