Last active
February 20, 2020 20:01
-
-
Save Guiorgy/be82502f7b839026421014ba8838e7c1 to your computer and use it in GitHub Desktop.
An Android SeekBar, that's flipped horizontally
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 ReverseSeekBar : SeekBar { | |
| constructor(context: Context) : super(context) { | |
| init() | |
| } | |
| constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { | |
| init() | |
| } | |
| constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) { | |
| init() | |
| } | |
| private var first = true | |
| override fun onTouchEvent(event: MotionEvent): Boolean { | |
| event.setLocation(this.width - event.x, event.y) | |
| return super.onTouchEvent(event) | |
| } | |
| override fun getProgress(): Int { | |
| return max - super.getProgress() + min | |
| } | |
| override fun setProgress(progress: Int) { | |
| super.setProgress(max - progress + min) | |
| } | |
| override fun onDraw(canvas: Canvas?) { | |
| if (first) { | |
| first = false | |
| val old = progress | |
| progress = min + max - progress | |
| super.onDraw(canvas) | |
| progress = old | |
| } else | |
| super.onDraw(canvas) | |
| } | |
| private fun init() { | |
| rotation = 180f | |
| } | |
| } | |
| /* | |
| <ReverseSeekBar | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:min="-50" | |
| android:max="50" | |
| android:progress="50"/> | |
| */ |
Author
Guiorgy
commented
Feb 20, 2020

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