@@ -18,12 +18,21 @@ import kotlinx.android.synthetic.main.layout_checkable_text.view.*
1818
1919class CheckableTextView : RelativeLayout {
2020
21+
22+ companion object {
23+ const val SCALE = 0
24+ const val TRANSLATE = 1
25+ }
26+
27+ private val defaultAnimDuration: Long = 250
2128 private var isChecked: Boolean = true
2229 private var listener: CheckedListener ? = null
2330 private val defaultCheckIcon = R .drawable.ic_check_circle_vector
2431 private val defaultTextColor = android.R .color.black
2532 private val defaultIconTintColor = android.R .color.transparent
2633 private var checkIcon = defaultCheckIcon
34+ private var animateStyle = SCALE
35+ private var animDuration: Long = defaultAnimDuration
2736
2837 constructor (context: Context ) : super (context) {
2938 init (context, null )
@@ -65,13 +74,16 @@ class CheckableTextView : RelativeLayout {
6574 val textStyle = array.getResourceId(R .styleable.CheckableTextView_ctv_TextStyle , 0 )
6675 checkIcon = array.getResourceId(R .styleable.CheckableTextView_ctv_Icon , 0 )
6776 val gravity = array.getInt(R .styleable.CheckableTextView_ctv_TextGravity , Gravity .CENTER )
77+ animateStyle = array.getInt(R .styleable.CheckableTextView_ctv_AnimType , SCALE )
78+ val animDuration =
79+ array.getInt(R .styleable.CheckableTextView_ctv_AnimDuration , defaultAnimDuration.toInt()).toLong()
80+ setAnimDuration(animDuration)
6881
6982 // giving applied style attrs least preference (colors n text size will be override by ctv_TextColor & ctv_TextSize as applied later)
7083 applyTextStyle(textStyle, context)
7184 validateCheckIcon(context)
72- checkedTextTV.text = text
73- checkedTextTV.isSelected = true
74- checkedTextTV.gravity = gravity
85+ setText(text ? : " " )
86+ setTextGravity(gravity)
7587 checkedTextTV.setTextColor(textColor)
7688 checkedIV.setImageResource(checkIcon)
7789
@@ -109,10 +121,27 @@ class CheckableTextView : RelativeLayout {
109121
110122 private fun animateView (view : View , show : Boolean ) {
111123 view.clearAnimation()
112- val scale = if (show) 1f else 0f
113- val rotation = if (show) 360f else - 360f
114- view.animate().setStartDelay(20 ).scaleX(scale).scaleY(scale).rotation(rotation).setDuration(250 )
115- .start()
124+
125+ when (animateStyle) {
126+ SCALE -> {
127+ view.translationX = 0f
128+ val scale = if (show) 1f else 0f
129+ val rotation = if (show) 0f else - 360f
130+ view.animate().setStartDelay(20 ).scaleX(scale).scaleY(scale).rotation(rotation)
131+ .setDuration(animDuration)
132+ .start()
133+ }
134+ TRANSLATE -> {
135+ view.scaleX = 1f
136+ view.scaleY = 1f
137+ val translate = if (show) 0f else (view.width.toFloat() + view.width / 2 )
138+ val rotation = if (show) 0f else 360f
139+ view.animate().setStartDelay(20 ).translationX(translate).rotation(rotation).setDuration(animDuration)
140+ .start()
141+ }
142+
143+ }
144+
116145 }
117146
118147 private fun validateCheckIcon (context : Context ) {
@@ -209,8 +238,10 @@ class CheckableTextView : RelativeLayout {
209238 }
210239
211240 fun setText (text : String ) {
212- if (emptyNullCheck(text))
241+ if (emptyNullCheck(text)) {
213242 checkedTextTV.text = text
243+ checkedTextTV.isSelected = true
244+ }
214245 }
215246
216247 fun setTextGravity (gravity : Int ) {
@@ -229,4 +260,21 @@ class CheckableTextView : RelativeLayout {
229260 if (isValidRes(resId))
230261 applyTextStyle(resId, context)
231262 }
263+
264+ /* *
265+ * @param animType should be [SCALE] OR [TRANSLATE]
266+ */
267+ fun setAnimStyle (animType : Int ) {
268+ animateStyle = when (animType) {
269+ SCALE -> SCALE
270+ TRANSLATE -> TRANSLATE
271+ else -> SCALE
272+ }
273+ }
274+
275+ fun setAnimDuration (duration : Long ) {
276+ if (duration.toInt() == 0 || duration < 0 ) return
277+ animDuration = duration
278+ }
279+
232280}
0 commit comments