Skip to content

Instantly share code, notes, and snippets.

@truedem
Created April 14, 2021 03:40
Show Gist options
  • Save truedem/43936fe8badcfb45c0b3843b7e9dc1c6 to your computer and use it in GitHub Desktop.
Save truedem/43936fe8badcfb45c0b3843b7e9dc1c6 to your computer and use it in GitHub Desktop.
Vertical SeekBar for Android
class VerticalSeekBar @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatSeekBar(context, attrs, defStyleAttr) {
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(h, w, oldh, oldw)
}
override fun setProgress(progress: Int) {
val oldWidth = width
val oldHeight = height
super.setProgress(progress)
onSizeChanged(width, height, oldWidth, oldHeight)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec)
setMeasuredDimension(measuredHeight, measuredWidth)
}
override fun onDraw(canvas: Canvas) {
canvas.rotate(-90f)
canvas.translate(-height.toFloat(), 0f)
super.onDraw(canvas)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
if (!isEnabled) {
return false
}
when (event.action) {
ACTION_DOWN, ACTION_MOVE, ACTION_UP -> {
progress = max - (max * event.y / height).toInt()
}
}
return true
}
}
@truedem
Copy link
Author

truedem commented Apr 14, 2021

Source (c) /user/SoMuchSpaceJunk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment