Skip to content

Commit 6d47f8b

Browse files
committed
add letterSpacingExtra attribute
1 parent 744f912 commit 6d47f8b

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

app/src/main/java/com/yy/mobile/rollingtext/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public void onAnimationEnd(Animator animation) {
124124

125125
final RollingTextView timeView = findViewById(R.id.timeView);
126126
timeView.setAnimationDuration(300);
127+
timeView.setLetterSpacingExtra(10);
127128
@SuppressLint("SimpleDateFormat") final DateFormat format = new SimpleDateFormat("HH:mm:ss");
128129
handler.post(new Runnable() {
129130
@Override

rollingtextview/src/main/java/com/yy/mobile/rollingtextview/RollingTextView.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,15 @@ open class RollingTextView : View {
299299
}
300300
}
301301

302+
/**
303+
* px between letter
304+
*/
305+
var letterSpacingExtra: Int
306+
set(value) {
307+
textManager.letterSpacingExtra = value
308+
}
309+
get() = textManager.letterSpacingExtra
310+
302311
override fun getBaseline(): Int {
303312
val fontMetrics = textPaint.fontMetrics
304313
return (textManager.textHeight / 2 + ((fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent)).toInt()

rollingtextview/src/main/java/com/yy/mobile/rollingtextview/TextManager.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ internal class TextManager(
2626

2727
private var charListColumns: List<List<Char>> = Collections.emptyList()
2828

29+
var letterSpacingExtra: Int = 0
30+
2931
init {
3032
updateFontMatrics()
3133
}
@@ -68,12 +70,18 @@ internal class TextManager(
6870
fun draw(canvas: Canvas) {
6971
textColumns.forEach {
7072
it.draw(canvas)
71-
canvas.translate(it.currentWidth, 0f)
73+
canvas.translate(it.currentWidth + letterSpacingExtra, 0f)
7274
}
7375
}
7476

7577
val currentTextWidth: Float
76-
get() = textColumns.map { it.currentWidth }.fold(0f) { total, next -> total + next }
78+
get() {
79+
val space = letterSpacingExtra * Math.max(0, textColumns.size - 1)
80+
val textWidth = textColumns
81+
.map { it.currentWidth }
82+
.fold(0f) { total, next -> total + next }
83+
return textWidth + space
84+
}
7785

7886
private fun Float.isZero(): Boolean = this < FLT_EPSILON && this > -FLT_EPSILON
7987

0 commit comments

Comments
 (0)