Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw NeumorphTextView in edit mode #59

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Draw NeumorphTextView in edit mode
  • Loading branch information
fornewid committed Nov 13, 2020
commit 29b17f0e6a0e0e085d973a0e918d833234c7440c
15 changes: 7 additions & 8 deletions neumorphism/src/main/java/soup/neumorphism/NeumorphTextView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class NeumorphTextView @JvmOverloads constructor(
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
lastTextCache = buildTextCache(w, h).also { origin ->
lastShadowLight = origin.generateBitmapShadowCache(w, h, shadowColorLight)
lastShadowDark = origin.generateBitmapShadowCache(w, h, shadowColorDark)
lastShadowLight = origin.generateBitmapShadowCache(w, h, shadowColorLight, isInEditMode)
lastShadowDark = origin.generateBitmapShadowCache(w, h, shadowColorDark, isInEditMode)
}
}

Expand All @@ -71,10 +71,7 @@ class NeumorphTextView @JvmOverloads constructor(
tp.color = Color.BLACK
tp.textSize = textSize
tp.typeface = typeface
if (isInEditMode.not()) {
// layout preview is NOT supported in now.
staticLayout(text, tp).draw(Canvas(this))
}
staticLayout(text, tp).draw(Canvas(this))
}
}

Expand All @@ -92,7 +89,7 @@ class NeumorphTextView @JvmOverloads constructor(
}

private fun Bitmap.generateBitmapShadowCache(
w: Int, h: Int, color: Int, radius: Float = 5f
w: Int, h: Int, color: Int, isInEditMode: Boolean = false
): Bitmap? {
val bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
Expand All @@ -105,7 +102,9 @@ class NeumorphTextView @JvmOverloads constructor(
// Draws the shadow from original drawable
val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
paint.color = color
paint.maskFilter = BlurMaskFilter(max(1f, radius), BlurMaskFilter.Blur.NORMAL)
if (isInEditMode.not()) {
paint.maskFilter = BlurMaskFilter(5f, BlurMaskFilter.Blur.NORMAL)
}
val offset = IntArray(2)
val shadow = bitmap.extractAlpha(paint, offset)
paint.maskFilter = null
Expand Down