Skip to content

Commit

Permalink
Added SmoothSeekBar
Browse files Browse the repository at this point in the history
  • Loading branch information
svignesh93 committed Nov 10, 2020
1 parent 2bb5909 commit 6683513
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
111 changes: 111 additions & 0 deletions widgetslib/src/main/kotlin/com/litekite/widget/SmoothSeekBar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2020 LiteKite Startup. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.litekite.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.appcompat.widget.AppCompatSeekBar
import kotlin.math.abs
import kotlin.math.roundToInt

/**
* @author Vignesh S
* @version 1.0, 06/11/2020
* @since 1.0
*/
class SmoothSeekBar @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatSeekBar(context, attrs, defStyleAttr) {

private var callback: OnSeekBarChangeListener? = null
private var lastProgress = 0

@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
if (!isEnabled) {
return false
}
when (event.action) {
MotionEvent.ACTION_DOWN -> {
lastProgress = calculateProgress(event)
callback?.onStartTrackingTouch(this)
}
MotionEvent.ACTION_MOVE -> {
isPressed = true
val newProgress = calculateProgress(event)
makeProgress(newProgress)
callback?.onProgressChanged(this, progress, true)
lastProgress = newProgress
}
MotionEvent.ACTION_UP -> {
isPressed = false
performClick()
callback?.onStopTrackingTouch(this)
}
MotionEvent.ACTION_CANCEL -> {
isPressed = false
callback?.onStopTrackingTouch(this)
}
}
return true
}

override fun setOnSeekBarChangeListener(l: OnSeekBarChangeListener?) {
callback = l
}

override fun getMin(): Int {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
return super.getMin()
}
return 0
}

private fun makeProgress(newProgress: Int) {
val displacement = lastProgress - newProgress
val currentProgress = if (displacement < 0) {
// Progress increased
progress + abs(displacement)
} else {
// Progress reduced
progress - abs(displacement)
}
// Setting current progress
progress = when {
progress > max -> {
max
}
progress < min -> {
min
}
else -> {
currentProgress
}
}
}

private fun calculateProgress(event: MotionEvent): Int {
val scale = event.x / width
val range = max - min
return (scale * range + min).roundToInt()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import android.view.ViewGroup
* @version 1.0, 03/11/2020
* @since 1.0
*/
@Suppress("UNUSED")
class ViewRotator @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
Expand Down

0 comments on commit 6683513

Please sign in to comment.