diff --git a/README.md b/README.md index 475291e..e5d8b4f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ExTextView -Android Extended TextView (know in Kotlin 🤩) +Android Extended TextView (now in Kotlin 🤩) # Usage @@ -21,7 +21,7 @@ allprojects { Step 2. Add the dependency ``` gradle dependencies { - implementation 'com.github.zijing07:ExTextView:v1.0.0' + implementation 'com.github.zijing07:ExTextView:${LATEST_VERSION}' } ``` @@ -34,22 +34,16 @@ strikeThroughPainting = StrikeThroughPainting(tv) strikeThroughPainting.strikeThrough() // All Options -strikeThroughPainting // default to true - .cutTextEdge(cutEdge) // default to Color.BLACK - .color(strokeColor) // default to 2F px - .strokeWidth(strokeWidth) // default to StrikeThroughPainting.MODE_DEFAULT - .mode(StrikeThroughPainting.MODE_DEFAULT) // default to 0.65F - .linePosition(0.65f) // default to 0.6F, since the first line is calculated +strikeThroughPainting + .cutTextEdge(cutEdge) // default to true + .color(strokeColor) // default to Color.BLACK + .strokeWidth(strokeWidth) // default to 2F px + .mode(StrikeThroughPainting.MODE_DEFAULT) // default to StrikeThroughPainting.MODE_DEFAULT + .linePosition(0.65f) // default to 0.65F // differently to the following lines - .firstLinePosition(0.6f) // default to 1_000 milliseconds, aka 1s - .totalTime(10000L) // default to null - .callback(object : StrikeThroughPainting.StrikeThroughPaintingCallback { - override fun onStrikeThroughEnd() { - Snackbar.make( - findViewById(R.id.container), - "Callback after animation", Snackbar.LENGTH_LONG - ).show() - } + .firstLinePosition(0.6f) // default to 0.6F, since the first line is calculated + .totalTime(10000L) // default to 1_000 milliseconds, aka 1s + .callback{Snackbar.make(findViewById(R.id.container),"Callback after animation", Snackbar.LENGTH_LONG).show()} }) .strikeThrough() @@ -65,4 +59,4 @@ strikeThroughPainting.clearStrikeThrough() - What else to extend? Please feel free to tell via opening an issue. # Demo -![demo](demo.gif) \ No newline at end of file +![demo](demo.gif) diff --git a/extextview/src/main/java/lib/mozidev/me/extextview/StrikeThroughPainting.kt b/extextview/src/main/java/lib/mozidev/me/extextview/StrikeThroughPainting.kt index 4d85cb7..801c6db 100644 --- a/extextview/src/main/java/lib/mozidev/me/extextview/StrikeThroughPainting.kt +++ b/extextview/src/main/java/lib/mozidev/me/extextview/StrikeThroughPainting.kt @@ -1,3 +1,5 @@ +@file:Suppress("unused") + package lib.mozidev.me.extextview import android.animation.Animator @@ -26,7 +28,9 @@ class StrikeThroughPainting constructor(private val targetView: ExTextView) : IP private var strikeThroughTotalTime = STRIKE_THROUGH_TOTAL_TIME private var strikeThroughMode = STRIKE_THROUGH_MODE private var strikeThroughCutTextEdge = STRIKE_THROUGH_CUT_TEXT_EDGE - + var isStriked: Boolean = false + private set + /** * Set strike through line position * @param percentageOfHeight set position of the drawing line, percentage marks the @@ -88,8 +92,15 @@ class StrikeThroughPainting constructor(private val targetView: ExTextView) : IP * @param callback StrikeThroughPaintingCallback * @return this */ - fun callback(callback: StrikeThroughPaintingCallback): StrikeThroughPainting { - strikeThroughPaintingCallback = callback + fun callback(callback: (() -> Unit)?): StrikeThroughPainting { + strikeThroughPaintingCallback = null + callback?.let { + strikeThroughPaintingCallback = object : StrikeThroughPaintingCallback { + override fun onStrikeThroughEnd() { + callback.invoke() + } + } + } return this } @@ -120,16 +131,15 @@ class StrikeThroughPainting constructor(private val targetView: ExTextView) : IP * Start strike through animation */ fun strikeThrough() { - targetView.post { - prepareAnimation() - startAnimation() - } + prepareAnimation() + startAnimation() } /** * Dismiss the strikeThrough line */ fun clearStrikeThrough() { + isStriked = false drawStrikeThrough = false strikeThroughProgress = 0f targetView.invalidate() @@ -166,7 +176,9 @@ class StrikeThroughPainting constructor(private val targetView: ExTextView) : IP * @param distance How long the strike through line should be, -1 indicates a full line draw * @return The length of strike through line drawn */ - private fun drawStrikeThroughLine(canvas: Canvas, lineIndex: Int, distance: Float = -1f): Float { + private fun drawStrikeThroughLine( + canvas: Canvas, lineIndex: Int, distance: Float = -1f + ): Float { var lIndex = lineIndex val rect = lineRects[lIndex] // float linePosition = lineIndex == 0 ? strikeThroughFirstLinePosition : strikeThroughPosition; @@ -260,6 +272,7 @@ class StrikeThroughPainting constructor(private val targetView: ExTextView) : IP animator.addListener(object : AnimatorListenerAdapter() { override fun onAnimationEnd(animation: Animator) { super.onAnimationEnd(animation) + isStriked = true //since we only animate on strike strikeThroughPaintingCallback?.onStrikeThroughEnd() } }) @@ -305,4 +318,4 @@ class StrikeThroughPainting constructor(private val targetView: ExTextView) : IP paint.color = strikeThroughColor paint.strokeWidth = strikeThroughStrokeWidth } -} \ No newline at end of file +} diff --git a/sample/src/main/java/lib/mozidev/me/demo/MainActivity.kt b/sample/src/main/java/lib/mozidev/me/demo/MainActivity.kt index a5b9fc0..5b42918 100644 --- a/sample/src/main/java/lib/mozidev/me/demo/MainActivity.kt +++ b/sample/src/main/java/lib/mozidev/me/demo/MainActivity.kt @@ -81,23 +81,15 @@ class MainActivity : AppCompatActivity() { } binding.button5.setOnClickListener { reset(binding.extendedTextView) - strikeThroughPainting // default to true - .cutTextEdge(cutEdge) // default to Color.BLACK - .color(strokeColor) // default to 2F px - .strokeWidth(strokeWidth) // default to StrikeThroughPainting.MODE_DEFAULT - .mode(StrikeThroughPainting.MODE_DEFAULT) // default to 0.65F - .linePosition(0.65f) // default to 0.6F, since the first line is calculated - // differently to the following lines - .firstLinePosition(0.6f) // default to 1_000 milliseconds, aka 1s - .totalTime(10000L) // default to null - .callback(object : StrikeThroughPainting.StrikeThroughPaintingCallback { - override fun onStrikeThroughEnd() { - Snackbar.make( - findViewById(R.id.container), - "Callback after animation", Snackbar.LENGTH_LONG - ).show() - } - }) + strikeThroughPainting + .cutTextEdge(cutEdge) + .color(strokeColor) + .strokeWidth(strokeWidth) + .linePosition(0.65f) + .callback { Snackbar.make( + findViewById(R.id.container), + "Callback after animation", Snackbar.LENGTH_LONG + ).show()} .strikeThrough() } binding.button4.setOnClickListener { reset(binding.extendedTextView) } @@ -123,4 +115,4 @@ class MainActivity : AppCompatActivity() { "Early work on the Aperture Science Handheld Portal Device began; the early version, called the Aperture Science Portable Quantum Tunneling Device, proved to be too bulky for effective use..." private val strs = listOf(SINGLE_LINE_TEXT, MULTIPLE_LINES_TEXT, VERY_LONG_TEXT) } -} \ No newline at end of file +}