@@ -12,6 +12,22 @@ import android.text.TextPaint
1212import android.util.AttributeSet
1313import android.util.TypedValue
1414import 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+
1531
1632/* *
1733 * 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
3147 private var maxLines: Int = 0
3248 private var initialized = false
3349 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+ }
3463
3564 private interface SizeTester {
3665 /* *
@@ -95,9 +124,76 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
95124 // else, too big
96125 }
97126 }
127+ setUpAnimator();
128+
98129 initialized = true
99130 }
100131
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+
101197 fun isValidWordWrap (before : Char , after : Char ): Boolean {
102198 return before == ' ' || before == ' -' || before == ' \n '
103199 }
0 commit comments