【发布时间】:2020-02-20 23:59:31
【问题描述】:
使用 Android SeekBar,您通常可以向左或向右拖动拇指,黄色进度颜色在拇指的左侧。我想要完全相反,基本上将黄色进度颜色翻转到拇指右侧并在 y 轴上翻转整个 SeekBar。
谁能指出我正确的方向?谢谢!
【问题讨论】:
使用 Android SeekBar,您通常可以向左或向右拖动拇指,黄色进度颜色在拇指的左侧。我想要完全相反,基本上将黄色进度颜色翻转到拇指右侧并在 y 轴上翻转整个 SeekBar。
谁能指出我正确的方向?谢谢!
【问题讨论】:
在摆弄了一些代码之后,这就是我得到的,它似乎工作得很好。希望它会在未来对其他人有所帮助。
public class ReversedSeekBar extends SeekBar {
public ReversedSeekBar(Context context) {
super(context);
}
public ReversedSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ReversedSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
float px = this.getWidth() / 2.0f;
float py = this.getHeight() / 2.0f;
canvas.scale(-1, 1, px, py);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
event.setLocation(this.getWidth() - event.getX(), event.getY());
return super.onTouchEvent(event);
}
}
这是在这两个问题的帮助下放在一起的:
【讨论】:
你试过 seekbar.setRotation(180) 吗?它将搜索栏翻转 180 度并倒置,这意味着左侧为最大,右侧为 0,拇指右侧的颜色。无需以这种方式创建自定义搜索栏。
【讨论】:
You should look into making a custom progress bar. 考虑到您想要做什么,您已经在 Android SDK 中拥有了所需的图像。我会提取它们并相应地编辑它们。 Here's a tutorial to help get you started.
【讨论】:
你试过在xml中设置这个
android:rotationY="180"
【讨论】:
这应该可以解决@mhenry 回答的一些问题
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()
}
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
}
}
【讨论】: