1
+ /*
2
+ * Copyright 2020 LiteKite Startup. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package com.litekite.widget
18
+
19
+ import android.annotation.SuppressLint
20
+ import android.content.Context
21
+ import android.util.AttributeSet
22
+ import android.view.MotionEvent
23
+ import androidx.appcompat.widget.AppCompatSeekBar
24
+ import kotlin.math.abs
25
+ import kotlin.math.roundToInt
26
+
27
+ /* *
28
+ * @author Vignesh S
29
+ * @version 1.0, 06/11/2020
30
+ * @since 1.0
31
+ */
32
+ class SmoothSeekBar @JvmOverloads constructor(
33
+ context : Context ,
34
+ attrs : AttributeSet ? = null ,
35
+ defStyleAttr : Int = 0
36
+ ) : AppCompatSeekBar(context, attrs, defStyleAttr) {
37
+
38
+ private var callback: OnSeekBarChangeListener ? = null
39
+ private var lastProgress = 0
40
+
41
+ @SuppressLint(" ClickableViewAccessibility" )
42
+ override fun onTouchEvent (event : MotionEvent ): Boolean {
43
+ if (! isEnabled) {
44
+ return false
45
+ }
46
+ when (event.action) {
47
+ MotionEvent .ACTION_DOWN -> {
48
+ lastProgress = calculateProgress(event)
49
+ callback?.onStartTrackingTouch(this )
50
+ }
51
+ MotionEvent .ACTION_MOVE -> {
52
+ isPressed = true
53
+ val newProgress = calculateProgress(event)
54
+ makeProgress(newProgress)
55
+ callback?.onProgressChanged(this , progress, true )
56
+ lastProgress = newProgress
57
+ }
58
+ MotionEvent .ACTION_UP -> {
59
+ isPressed = false
60
+ performClick()
61
+ callback?.onStopTrackingTouch(this )
62
+ }
63
+ MotionEvent .ACTION_CANCEL -> {
64
+ isPressed = false
65
+ callback?.onStopTrackingTouch(this )
66
+ }
67
+ }
68
+ return true
69
+ }
70
+
71
+ override fun setOnSeekBarChangeListener (l : OnSeekBarChangeListener ? ) {
72
+ callback = l
73
+ }
74
+
75
+ override fun getMin (): Int {
76
+ if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .O ) {
77
+ return super .getMin()
78
+ }
79
+ return 0
80
+ }
81
+
82
+ private fun makeProgress (newProgress : Int ) {
83
+ val displacement = lastProgress - newProgress
84
+ val currentProgress = if (displacement < 0 ) {
85
+ // Progress increased
86
+ progress + abs(displacement)
87
+ } else {
88
+ // Progress reduced
89
+ progress - abs(displacement)
90
+ }
91
+ // Setting current progress
92
+ progress = when {
93
+ progress > max -> {
94
+ max
95
+ }
96
+ progress < min -> {
97
+ min
98
+ }
99
+ else -> {
100
+ currentProgress
101
+ }
102
+ }
103
+ }
104
+
105
+ private fun calculateProgress (event : MotionEvent ): Int {
106
+ val scale = event.x / width
107
+ val range = max - min
108
+ return (scale * range + min).roundToInt()
109
+ }
110
+
111
+ }
0 commit comments