Created
April 14, 2021 03:40
-
-
Save truedem/43936fe8badcfb45c0b3843b7e9dc1c6 to your computer and use it in GitHub Desktop.
Vertical SeekBar for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source (c) /user/SoMuchSpaceJunk