Skip to content

Commit

Permalink
Merge pull request #2 from GuilhE/master
Browse files Browse the repository at this point in the history
Minor improvements
  • Loading branch information
zijing07 authored May 4, 2020
2 parents f08a9a2 + 24a8713 commit bfb5903
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 45 deletions.
30 changes: 12 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ExTextView

Android Extended TextView (know in Kotlin 🤩)
Android Extended TextView (now in Kotlin 🤩)

# Usage

Expand All @@ -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}'
}
```

Expand All @@ -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()

Expand All @@ -65,4 +59,4 @@ strikeThroughPainting.clearStrikeThrough()
- What else to extend? Please feel free to tell via opening an issue.

# Demo
![demo](demo.gif)
![demo](demo.gif)
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("unused")

package lib.mozidev.me.extextview

import android.animation.Animator
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
}
})
Expand Down Expand Up @@ -305,4 +318,4 @@ class StrikeThroughPainting constructor(private val targetView: ExTextView) : IP
paint.color = strikeThroughColor
paint.strokeWidth = strikeThroughStrokeWidth
}
}
}
28 changes: 10 additions & 18 deletions sample/src/main/java/lib/mozidev/me/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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)
}
}
}

0 comments on commit bfb5903

Please sign in to comment.