-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleItemAnimator.kt
52 lines (42 loc) · 1.7 KB
/
SampleItemAnimator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.example.vsbottomnavigationview
import android.animation.ValueAnimator
import android.content.Context
import android.view.MenuItem
import android.widget.TextView
import com.wshtaits.vsbottomnavigationview.ItemAnimator
import com.wshtaits.vsbottomnavigationview.ItemViewHolder
import kotlinx.android.synthetic.main.item_sample.*
open class SampleItemAnimator(context: Context) : ItemAnimator() {
private val selectedColor = context.getColor(android.R.color.black)
private val unselectedColor = context.getColor(android.R.color.darker_gray)
private var titleTextSize: Float = 12f
override fun ItemViewHolder.onCreate(menuItem: MenuItem) {
icon_iv.background = menuItem.icon
title_tv.text = menuItem.title
title_tv.setTextColor(selectedColor)
if (menuItem.isChecked) {
menuItem.icon.setTint(selectedColor)
} else {
menuItem.icon.setTint(unselectedColor)
title_tv.textSize = 0f
}
}
override fun ItemViewHolder.onSelect(menuItem: MenuItem) {
menuItem.icon.setTint(selectedColor)
animateTextSize(title_tv, 0f, titleTextSize)
}
override fun ItemViewHolder.onDeselect(menuItem: MenuItem) {
menuItem.icon.setTint(unselectedColor)
animateTextSize(title_tv, titleTextSize, 0f)
}
private fun animateTextSize(textView: TextView, fromSize: Float, toSize: Float) {
with(ValueAnimator.ofFloat(0f, 1f)) {
addUpdateListener { valueAnimator ->
val percent = valueAnimator.animatedValue as Float
textView.textSize = fromSize - (fromSize - toSize) * percent
}
duration = 300
start()
}
}
}