-
Notifications
You must be signed in to change notification settings - Fork 10
/
CurvedBottomNavigation.kt
471 lines (420 loc) · 15.2 KB
/
CurvedBottomNavigation.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
@file:Suppress("unused")
package com.qamar.curvedbottomnaviagtion
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.os.Build
import android.util.AttributeSet
import android.util.LayoutDirection
import android.util.Log
import android.view.Gravity
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.annotation.RequiresApi
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
import androidx.navigation.NavController
import com.qamar.curvedbottomnaviagtion.enum.NavigationType
import kotlin.math.abs
internal typealias IBottomNavigationListener = (model: CurvedBottomNavigation.Model) -> Unit
@Suppress("MemberVisibilityCanBePrivate")
class CurvedBottomNavigation : FrameLayout {
var models = ArrayList<Model>()
var cells = ArrayList<BottomNaviagtionItem>()
private set
private var callListenerWhenIsSelected = false
private var selectedId = -1
private var onClickedListener: IBottomNavigationListener = {}
private var onShowListener: IBottomNavigationListener = {}
private var onReselectListener: IBottomNavigationListener = {}
private lateinit var navController: NavController
private var isAnimating = false
var iconColor = Color.parseColor("#757575")
set(value) {
field = value
updateAllIfAllowDraw()
}
var heightBottomNaviagtion = 86.dp(context)
set(value) {
field = value
updateAllIfAllowDraw()
}
var navigationType = NavigationType.LABELED
set(value) {
field = value
updateAllIfAllowDraw()
}
var titleColor = Color.parseColor("#ffffff")
set(value) {
field = value
updateAllIfAllowDraw()
}
var titleTextSize = 12
set(value) {
field = value
updateAllIfAllowDraw()
}
var iconSize = 48f
set(value) {
field = value
updateAllIfAllowDraw()
}
var selectedIconSize = 48f
set(value) {
field = value
updateAllIfAllowDraw()
}
var selectedIconColor = Color.parseColor("#2196f3")
set(value) {
field = value
updateAllIfAllowDraw()
}
var backgroundBottomColor = Color.parseColor("#ffffff")
set(value) {
field = value
updateAllIfAllowDraw()
}
var curveRadius = 72f
set(value) {
field = value
updateAllIfAllowDraw()
}
var fabColor = Color.parseColor("#ffffff")
set(value) {
field = value
updateAllIfAllowDraw()
}
private var shadowColor = -0x454546
var badgeTextColor = Color.parseColor("#ffffff")
set(value) {
field = value
updateAllIfAllowDraw()
}
var badgeBackgroundColor = Color.parseColor("#ff0000")
set(value) {
field = value
updateAllIfAllowDraw()
}
var badgeFontType: Typeface? = null
set(value) {
field = value
updateAllIfAllowDraw()
}
var titleFontType: Typeface? = null
set(value) {
field = value
updateAllIfAllowDraw()
}
var hasAnimation: Boolean = true
set(value) {
field = value
updateAllIfAllowDraw()
}
private var rippleColor = Color.parseColor("#757575")
private var allowDraw = false
@Suppress("PrivatePropertyName")
private lateinit var ll_cells: LinearLayout
private lateinit var curvedView: CurvedView
constructor(context: Context) : super(context) {
initializeViews()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
setAttributeFromXml(context, attrs)
initializeViews()
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
setAttributeFromXml(context, attrs)
initializeViews()
}
@RequiresApi(Build.VERSION_CODES.O)
private fun setAttributeFromXml(context: Context, attrs: AttributeSet) {
val a = context.theme.obtainStyledAttributes(
attrs,
R.styleable.CurvedBottomNavigationView, 0, 0
)
try {
a.apply {
titleColor = getColor(
R.styleable.CurvedBottomNavigationView_cbn_titleColor,
titleColor
)
titleTextSize = getColor(
R.styleable.CurvedBottomNavigationView_cbn_titleTextSize,
titleTextSize
)
iconColor = getColor(
R.styleable.CurvedBottomNavigationView_cbn_iconColor,
iconColor
)
selectedIconColor = getColor(
R.styleable.CurvedBottomNavigationView_cbn_selectedIconColor,
selectedIconColor
)
backgroundBottomColor = getColor(
R.styleable.CurvedBottomNavigationView_cbn_background,
backgroundBottomColor
)
heightBottomNaviagtion = getDimensionPixelSize(
R.styleable.CurvedBottomNavigationView_cbn_height,
heightBottomNaviagtion
)
iconSize = getDimensionPixelSize(
R.styleable.CurvedBottomNavigationView_cbn_icon_size,
iconSize.toInt()
).toFloat()
selectedIconSize = getDimensionPixelSize(
R.styleable.CurvedBottomNavigationView_cbn_selected_icon_size,
selectedIconSize.toInt()
).toFloat()
curveRadius = getDimensionPixelSize(
R.styleable.CurvedBottomNavigationView_cbn_curve_radius,
curveRadius.toInt()
).toFloat()
fabColor =
getColor(R.styleable.CurvedBottomNavigationView_cbn_fabColor, fabColor)
badgeTextColor =
getColor(
R.styleable.CurvedBottomNavigationView_cbn_badgeTextColor,
badgeTextColor
)
badgeBackgroundColor = getColor(
R.styleable.CurvedBottomNavigationView_cbn_badgeBackgroundColor,
badgeBackgroundColor
)
rippleColor =
getColor(R.styleable.CurvedBottomNavigationView_cbn_rippleColor, rippleColor)
shadowColor =
getColor(R.styleable.CurvedBottomNavigationView_cbn_shadowColor, shadowColor)
val typeface = getFont(R.styleable.CurvedBottomNavigationView_cbn_badgeFont)
if (typeface != null)
badgeFontType = typeface
val typefaceTitle = getFont(R.styleable.CurvedBottomNavigationView_cbn_badgeFont)
if (typefaceTitle != null)
titleFontType = typefaceTitle
hasAnimation =
getBoolean(
R.styleable.CurvedBottomNavigationView_cbn_hasAnimation,
hasAnimation
)
}
} finally {
a.recycle()
}
}
private fun initializeViews() {
ll_cells = LinearLayout(context)
ll_cells.apply {
val params =
LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, heightBottomNaviagtion)
params.gravity = Gravity.BOTTOM
layoutParams = params
orientation = LinearLayout.HORIZONTAL
clipChildren = false
clipToPadding = false
}
curvedView = CurvedView(context)
curvedView.curveX = curveRadius
Log.e("qmrCurve","${curveRadius}")
curvedView.apply {
curveWidth = curveRadius
layoutParams =
LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, heightBottomNaviagtion)
color = backgroundBottomColor
curveX = curveRadius
shadowColor = this@CurvedBottomNavigation.shadowColor
}
addView(curvedView)
addView(ll_cells)
allowDraw = true
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
if (selectedId == -1) {
curvedView.curveX =
if (Build.VERSION.SDK_INT >= 17 && layoutDirection == LayoutDirection.RTL) measuredWidth + 255f.dp(
context
) else (-255f).dp(context)
}
if (selectedId != -1) {
show(selectedId, false)
}
}
init {
}
fun add(model: Model) {
val cell = BottomNaviagtionItem(context)
cell.apply {
val params = LinearLayout.LayoutParams(0, heightBottomNaviagtion, 1f)
layoutParams = params
icon = model.icon
title = model.title
badge = model.count
titleTextSize = this@CurvedBottomNavigation.titleTextSize
titleColor = this@CurvedBottomNavigation.titleColor
iconColor = this@CurvedBottomNavigation.iconColor
selectedIconColor = this@CurvedBottomNavigation.selectedIconColor
circleColor = this@CurvedBottomNavigation.fabColor
badgeTextColor = this@CurvedBottomNavigation.badgeTextColor
badgeBackgroundColor = this@CurvedBottomNavigation.badgeBackgroundColor
badgeFont = this@CurvedBottomNavigation.badgeFontType
titleFontType = this@CurvedBottomNavigation.titleFontType
selectedIconSize = this@CurvedBottomNavigation.selectedIconSize
iconSize = this@CurvedBottomNavigation.iconSize
rippleColor = this@CurvedBottomNavigation.rippleColor
onClickListener = {
if (isShowing(model.id)) // added for https://github.com/shetmobile/MeowBottomNavigation/issues/39
onReselectListener(model)
if (!cell.isItemSelected && !isAnimating) {
show(model.id, hasAnimation)
onClickedListener(model)
} else {
if (callListenerWhenIsSelected)
onClickedListener(model)
}
}
unselectedItem(hasAnimation)
ll_cells.addView(this)
}
cells.add(cell)
models.add(model)
}
private fun updateAllIfAllowDraw() {
if (!allowDraw)
return
cells.forEach {
it.titleColor = titleColor
it.titleTextSize = titleTextSize
it.iconColor = iconColor
it.selectedIconColor = selectedIconColor
it.circleColor = fabColor
it.badgeTextColor = badgeTextColor
it.badgeBackgroundColor = badgeBackgroundColor
it.badgeFont = badgeFontType
it.iconSize = iconSize
it.selectedIconSize = selectedIconSize
}
curvedView.color = backgroundBottomColor
curvedView.curveWidth = curveRadius
val layoutParams = curvedView.layoutParams
layoutParams.height = heightBottomNaviagtion
curvedView.layoutParams = layoutParams
}
private fun anim(cell: BottomNaviagtionItem, id: Int, enableAnimation: Boolean = true) {
isAnimating = true
val pos = getModelPosition(id)
val nowPos = getModelPosition(selectedId)
val nPos = if (nowPos < 0) 0 else nowPos
val dif = abs(pos - nPos)
val d = (dif) * 100L + 150L
val animDuration = if (enableAnimation && hasAnimation) d else 1L
val animInterpolator = FastOutSlowInInterpolator()
val anim = ValueAnimator.ofFloat(0f, 1f)
anim.apply {
duration = animDuration
interpolator = animInterpolator
val beforeX = curvedView.curveX
addUpdateListener {
val f = it.animatedFraction
val newX = cell.x + (cell.measuredWidth / 2)
if (newX > beforeX)
curvedView.curveX = f * (newX - beforeX) + beforeX
else
curvedView.curveX = beforeX - f * (beforeX - newX)
if (f == 1f)
isAnimating = false
}
start()
}
if (abs(pos - nowPos) > 1) {
val progressAnim = ValueAnimator.ofFloat(0f, 1f)
progressAnim.apply {
duration = animDuration
interpolator = animInterpolator
addUpdateListener {
val f = it.animatedFraction
curvedView.progress = f * 2f
}
start()
}
}
cell.isFromLeft = pos > nowPos
cells.forEach {
it.duration = d
}
}
fun show(id: Int, enableAnimation: Boolean = true) {
for (i in models.indices) {
val model = models[i]
val cell = cells[i]
if (model.id == id) {
anim(cell, id, enableAnimation)
cell.selectedItem(enableAnimation)
onShowListener(model)
} else {
cell.unselectedItem(hasAnimation)
}
}
selectedId = id
}
fun setupNavController(navController: NavController) {
this.navController = navController
show(navController.currentDestination?.id!!)
navController.addOnDestinationChangedListener { controller, destination, arguments ->
show(destination.id, true)
}
}
fun isShowing(id: Int): Boolean {
return selectedId == id
}
fun getModelById(id: Int): Model? {
models.forEach {
if (it.id == id)
return it
}
return null
}
fun getCellById(id: Int): BottomNaviagtionItem? {
return cells[getModelPosition(id)]
}
fun getModelPosition(id: Int): Int {
for (i in models.indices) {
val item = models[i]
if (item.id == id)
return i
}
return -1
}
fun setCount(id: Int, count: String) {
val model = getModelById(id) ?: return
val pos = getModelPosition(id)
model.count = count
cells[pos].badge = count
}
fun clearCount(id: Int) {
val model = getModelById(id) ?: return
val pos = getModelPosition(id)
model.count = BottomNaviagtionItem.EMPTY_VALUE
cells[pos].badge = BottomNaviagtionItem.EMPTY_VALUE
}
fun clearAllCounts() {
models.forEach {
clearCount(it.id)
}
}
fun setOnShowListener(listener: IBottomNavigationListener) {
onShowListener = listener
}
fun setOnClickMenuListener(listener: IBottomNavigationListener) {
onClickedListener = listener
}
fun setOnReselectListener(listener: IBottomNavigationListener) {
onReselectListener = listener
}
class Model(var id: Int, var title: String, var icon: Int) {
var count: String = BottomNaviagtionItem.EMPTY_VALUE
}
}