Skip to content

Commit

Permalink
Experiment: Added scaling for shadows
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamim Attafi authored and Tamim Attafi committed Feb 20, 2021
1 parent cafb9dc commit 1c5433e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class NeumorphShapeDrawable : Drawable {
}

private fun updateShadowShape() {
val internalBounds = getBoundsInternal()
val internalBounds = this.getBoundsInternal()
if (internalBounds.width() <= 0 || internalBounds.height() <= 0) {
shadow = null
return
Expand Down Expand Up @@ -305,7 +305,8 @@ class NeumorphShapeDrawable : Drawable {
drawBackgroundBitmap(canvas)
}

shadow?.draw(canvas, outlinePath)
val shadowBounds = this.getBoundsInternal()
shadow?.draw(canvas, outlinePath, shadowBounds)

if (hasStroke()) {
drawStrokeShape(canvas)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ internal class BasinShape(drawableState: NeumorphShapeDrawableState) : Shape {
shadows.forEach { it.setDrawableState(newDrawableState) }
}

override fun draw(canvas: Canvas, outlinePath: Path) {
shadows.forEach { it.draw(canvas, outlinePath) }
override fun draw(canvas: Canvas, outlinePath: Path, bounds: Rect) {
shadows.forEach { it.draw(canvas, outlinePath, bounds) }
}

override fun updateShadowBitmap(bounds: Rect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class FlatShape(
this.drawableState = newDrawableState
}

override fun draw(canvas: Canvas, outlinePath: Path) {
override fun draw(canvas: Canvas, outlinePath: Path, bounds: Rect) {
canvas.withClipOut(outlinePath) {
val elevation = drawableState.shadowElevation
val z = drawableState.shadowElevation + drawableState.translationZ
Expand All @@ -36,13 +36,15 @@ internal class FlatShape(
val inset = drawableState.inset
left = inset.left.toFloat()
top = inset.top.toFloat()
lightShadowBitmap?.let {
lightShadowBitmap?.let { originalShadow ->
val offset = -elevation - z
drawBitmap(it, offset + left, offset + top, null)
val scaledShadow = Bitmap.createScaledBitmap(originalShadow, bounds.width() + (offset + left).toInt(), bounds.height() + (offset + top).toInt(), false)
drawBitmap(scaledShadow, offset + left, offset + top, null)
}
darkShadowBitmap?.let {
darkShadowBitmap?.let { originalShadow ->
val offset = -elevation + z
drawBitmap(it, offset + left, offset + top, null)
val scaledShadow = Bitmap.createScaledBitmap(originalShadow, bounds.width() + (offset + left).toInt(), bounds.height() + (offset + top).toInt(), false)
drawBitmap(scaledShadow, offset + left, offset + top, null)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class PressedShape(
this.drawableState = newDrawableState
}

override fun draw(canvas: Canvas, outlinePath: Path) {
override fun draw(canvas: Canvas, outlinePath: Path, bounds: Rect) {
canvas.withClip(outlinePath) {
shadowBitmap?.let {
val left: Float
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package soup.neumorphism.internal.shape
import android.graphics.Canvas
import android.graphics.Path
import android.graphics.Rect
import android.graphics.RectF
import soup.neumorphism.NeumorphShapeDrawable.NeumorphShapeDrawableState

internal interface Shape {
fun setDrawableState(newDrawableState: NeumorphShapeDrawableState)
fun draw(canvas: Canvas, outlinePath: Path)
fun draw(canvas: Canvas, outlinePath: Path, bounds: Rect)
fun updateShadowBitmap(bounds: Rect)
}

0 comments on commit 1c5433e

Please sign in to comment.