@@ -12,6 +12,22 @@ import android.text.TextPaint
12
12
import android.util.AttributeSet
13
13
import android.util.TypedValue
14
14
import androidx.appcompat.widget.AppCompatTextView
15
+ import android.animation.ValueAnimator
16
+ import java.text.DecimalFormat
17
+ import android.animation.Animator
18
+ import android.R.string.cancel
19
+ import android.animation.TimeInterpolator
20
+ import androidx.annotation.NonNull
21
+ import com.lb.auto_fit_textview.AutoResizeTextView.CountAnimationListener
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
15
31
16
32
/* *
17
33
* a textView that is able to self-adjust its font size depending on the min and max size of the font, and its own size.<br></br>
@@ -31,6 +47,19 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
31
47
private var maxLines: Int = 0
32
48
private var initialized = false
33
49
private var textPaint: TextPaint ? = null
50
+ private var isAnimating = false
51
+ lateinit var mCountAnimator: ValueAnimator
52
+ lateinit var mCountAnimationListener: CountAnimationListener
53
+ private var mCaptionString: String? = null
54
+ private val DEFAULT_DURATION : Long = 1000
55
+
56
+
57
+ // interface progress animationListener
58
+ interface CountAnimationListener {
59
+ fun onAnimationStart (animatedValue : Any )
60
+
61
+ fun onAnimationEnd (animatedValue : Any )
62
+ }
34
63
35
64
private interface SizeTester {
36
65
/* *
@@ -95,9 +124,76 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
95
124
// else, too big
96
125
}
97
126
}
127
+ setUpAnimator();
128
+
98
129
initialized = true
99
130
}
100
131
132
+ private fun setUpAnimator () {
133
+ mCountAnimator = ValueAnimator ()
134
+ mCountAnimator.addUpdateListener(ValueAnimator .AnimatorUpdateListener { animation ->
135
+ val value: String
136
+
137
+ value = if (mCaptionString == null ) {
138
+ animation.animatedValue.toString()
139
+ } else {
140
+ String .format(" %s\n %s" , animation.animatedValue, mCaptionString)
141
+ }
142
+ super .setText(value)
143
+ })
144
+
145
+ mCountAnimator.addListener(object : Animator .AnimatorListener {
146
+ override fun onAnimationStart (animation : Animator ) {
147
+ isAnimating = true
148
+
149
+ mCountAnimationListener.onAnimationStart(mCountAnimator.animatedValue)
150
+ }
151
+
152
+ override fun onAnimationEnd (animation : Animator ) {
153
+ isAnimating = false
154
+
155
+ mCountAnimationListener.onAnimationEnd(mCountAnimator.animatedValue)
156
+ }
157
+
158
+ override fun onAnimationCancel (animation : Animator ) {
159
+ // do nothing
160
+ }
161
+
162
+ override fun onAnimationRepeat (animation : Animator ) {
163
+ // do nothing
164
+ }
165
+ })
166
+ mCountAnimator.duration = DEFAULT_DURATION
167
+ }
168
+
169
+ override fun onDetachedFromWindow () {
170
+ super .onDetachedFromWindow()
171
+ mCountAnimator.cancel()
172
+ }
173
+
174
+ fun countAnimation (fromValue : Int , toValue : Int ) {
175
+ if (isAnimating) return
176
+
177
+ mCountAnimator.setIntValues(fromValue, toValue)
178
+ mCountAnimator.start()
179
+ }
180
+
181
+ fun setAnimationDuration (duration : Long ): AutoResizeTextView {
182
+ mCountAnimator.duration = duration
183
+ return this
184
+ }
185
+
186
+ fun setInterpolator (value : TimeInterpolator ): AutoResizeTextView {
187
+ mCountAnimator.interpolator = value
188
+ return this
189
+ }
190
+
191
+ fun setCountAnimationListener (mCountAnimationListener : CountAnimationListener ): AutoResizeTextView {
192
+ this .mCountAnimationListener = mCountAnimationListener
193
+ return this
194
+ }
195
+
196
+
101
197
fun isValidWordWrap (before : Char , after : Char ): Boolean {
102
198
return before == ' ' || before == ' -' || before == ' \n '
103
199
}
0 commit comments