@@ -5,13 +5,19 @@ import android.animation.ValueAnimator
55import android.animation.ValueAnimator.REVERSE
66import android.content.Context
77import android.graphics.Color
8+ import android.graphics.drawable.BitmapDrawable
89import android.util.AttributeSet
10+ import android.view.View
911import android.view.animation.AccelerateDecelerateInterpolator
1012import android.view.animation.Animation
1113import android.view.animation.AnimationUtils
1214import android.view.animation.Interpolator
1315import android.widget.FrameLayout
16+ import android.widget.ImageView
1417import android.widget.TextView
18+ import android.view.ViewGroup
19+
20+
1521
1622
1723/* *
@@ -24,11 +30,11 @@ class FSAnimatedTV(context: Context?, attrs: AttributeSet?) : FrameLayout(contex
2430 private var value: Int = 0
2531 private var outAnim: Animation
2632 private var inAnim: Animation
27- val color: Int
28- val duration: Long
29- val outInterpolator: Interpolator = AccelerateDecelerateInterpolator ()
30- val inInterpolator: Interpolator = AccelerateDecelerateInterpolator ()
31- val colorFeedback: Boolean
33+ var color: Int
34+ var duration: Long
35+ var outInterpolator: Interpolator = AccelerateDecelerateInterpolator ()
36+ var inInterpolator: Interpolator = AccelerateDecelerateInterpolator ()
37+ var colorFeedback: Boolean
3238
3339 private fun initialize (context : Context ) {
3440 inflate(context, R .layout.animated_tv, this )
@@ -40,18 +46,42 @@ class FSAnimatedTV(context: Context?, attrs: AttributeSet?) : FrameLayout(contex
4046 inAnim = AnimationUtils .loadAnimation(this .context, R .anim.trans_in_up)
4147
4248 val ta = context.obtainStyledAttributes(attrs, R .styleable.FSAnimatedTV , 0 , 0 )
43- val dimension = ta.getDimension(R .styleable.FSAnimatedTV_textSize , 40F )
49+ val dimension = ta.getDimension(R .styleable.FSAnimatedTV_textSize , 0F )
50+ val leftDrawableSize = ta.getDimension(R .styleable.FSAnimatedTV_leftDrawableSize , dimension)
51+ val leftDrawableHeight = ta.getDimension(R .styleable.FSAnimatedTV_leftDrawableHeight , leftDrawableSize)
52+ val leftDrawableWidth = ta.getDimension(R .styleable.FSAnimatedTV_leftDrawableWidth , leftDrawableSize)
53+ val leftDrawable = ta.getDrawable(R .styleable.FSAnimatedTV_leftDrawable )
54+ val leftDrawableTintColor = ta.getColor(R .styleable.FSAnimatedTV_leftDrawableTint , Color .BLACK )
4455 color = ta.getColor(R .styleable.FSAnimatedTV_textColor , Color .BLACK )
4556 duration = ta.getInt(R .styleable.FSAnimatedTV_duration , 300 ).toLong()
4657 colorFeedback = ta.getBoolean(R .styleable.FSAnimatedTV_colorFeedback , false )
4758
4859 val text = findViewById<TextView >(R .id.fs_number)
49- text.textSize = dimension
60+ text.textSize = dimension / 2F
5061 text.setTextColor(color)
5162
63+ if (leftDrawable != null ) {
64+ val leftIv = findViewById<ImageView >(R .id.iv_left_drawable)
65+ val params = leftIv.layoutParams
66+ params.height = leftDrawableHeight.toInt()
67+ params.width = leftDrawableWidth.toInt()
68+ leftIv.requestLayout()
69+ leftIv.setImageDrawable(leftDrawable)
70+ leftIv.visibility = View .VISIBLE
71+ leftIv.setColorFilter(leftDrawableTintColor)
72+ }
73+
5274 ta.recycle()
5375 }
5476
77+ fun increment (by : Int ) {
78+ setNum(value + by)
79+ }
80+
81+ fun decrement (by : Int ) {
82+ setNum(value - by)
83+ }
84+
5585 fun setNum (num : Int ) {
5686 if (num > value) {
5787 animate(num.toString(), DIRECTION .UPWARDS )
@@ -104,10 +134,16 @@ class FSAnimatedTV(context: Context?, attrs: AttributeSet?) : FrameLayout(contex
104134 FSAnimatedTV .DIRECTION .DOWNWARDS -> Color .RED
105135 }
106136 val text = findViewById<TextView >(R .id.fs_number)
137+ val leftIv = findViewById<ImageView >(R .id.iv_left_drawable)
107138 val colorAnimation = ValueAnimator .ofObject(ArgbEvaluator (), color, colorTo)
108139 colorAnimation.duration = duration * 2
109140 colorAnimation.interpolator = AccelerateDecelerateInterpolator ()
110- colorAnimation.addUpdateListener { animator -> text.setTextColor(animator.animatedValue as Int ) }
141+ colorAnimation.addUpdateListener { animator ->
142+ run {
143+ text.setTextColor(animator.animatedValue as Int )
144+ leftIv.setColorFilter(animator.animatedValue as Int )
145+ }
146+ }
111147 colorAnimation.repeatMode = REVERSE
112148 colorAnimation.repeatCount = 1
113149 colorAnimation.start()
0 commit comments