如何在 Android 中获得可用的垂直 SeekBar?
我已经在此处实现了常见的 VerticalSeekBar 帖子。实际上,SeekBar 的运行方式有点古怪。下面是我对该示例稍加修改的 onTouchEvent()
:
public boolean onTouchEvent(MotionEvent event)
{
xPos = event.getX();
yPos = event.getY();
oOffset = this.getThumbOffset();
oProgress = this.getProgress();
//Code from example - Not working
//this.setThumbOffset( progress * (this.getBottom()-this.getTop()) );
this.setProgress((int)(29*yPos/this.getBottom()));
return true;
}
我成功实现了一个 VerticalSeekBar,其中进度按预期更新并且功能齐全,但拇指不跟随。这只是一个图形故障,所以我现在忽略它。但是,如果能够实现这一点就太好了。此 SeekBar 的 max = 20 。
但是,我尝试使用 max = 1000
实现另一个 VerticalSeekBar。显然,它使用相同的代码,因此您会假设相同的行为。即使我的手指滑过 SeekBar 并最终离开屏幕,我也只能取得 0~35 的进度。如果我只是点击进度条末尾附近(应该是进度 ~ 900
),它会返回大约 35 的进度,黄色进度条通过停留来反映该值靠近顶部。
我的问题是:是否有人有一个可用的垂直 SeekBar 的链接,或知道如何适应这个特定的示例?
I've implemented the commonly-pointed-to VerticalSeekBar post here. As it is, the SeekBar operates a little quirky. Here is my slightly adapted onTouchEvent()
from that example:
public boolean onTouchEvent(MotionEvent event)
{
xPos = event.getX();
yPos = event.getY();
oOffset = this.getThumbOffset();
oProgress = this.getProgress();
//Code from example - Not working
//this.setThumbOffset( progress * (this.getBottom()-this.getTop()) );
this.setProgress((int)(29*yPos/this.getBottom()));
return true;
}
I've managed to implement one VerticalSeekBar in which the progress updates as expected and is fully-functional, but the thumb does not follow suit. This is only a graphical glitch, so I'm overlooking it for now. But, it would be nice to have that working. This SeekBar has max = 20
.
However, I tried implementing another VerticalSeekBar with max = 1000
. Obviously, it uses the same code, so you'd assume the same behavior. I'm only able to achieve a progress of 0~35, even as my finger slides beyond the SeekBar and eventually off the screen. If I just tap near the end of the progress bar (which should be progress ~ 900
) it returns a progress of about 35 and the yellow progress bar reflects that value by staying near the top.
My question is: Does anyone have a link to a working vertical SeekBar, or know how to adapt this particular example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我寻找的不是
SeekBar
,而是 VerticalSlider
。这是我在 Kotlin 中实现的垂直滑块。Instead of
SeekBar
, I was looking for VerticalSlider
. Here is my implementation for vertical Slider in Kotlin.这是一个有效的 VerticalSeekBar 实现:
要实现它,请在项目中创建一个新类,选择正确的包:
在那里,粘贴代码并保存。现在在您的 XML 布局中使用它:
Here is a working VerticalSeekBar implementation:
To implement it, create a new class in your project, choosing the right package:
There, paste the code and save it. Now use it in your XML layout:
接受的答案中给出的代码没有拦截 onStartTrackingTouch 和 onStopTrackingTouch 事件,因此我对其进行了修改以更好地控制这两个事件。
这是我的代码:
The code given in the accepted answer didn't intercept the onStartTrackingTouch and the onStopTrackingTouch events, so I've modified it to have more control over this two events.
Here is my code:
我在将这段代码与 setProgress 方法一起使用时遇到了问题。为了解决这些问题,我建议重写 setProgress 并向其添加 onSizeChanged 调用。
I had problems while using this code with setProgress method. To solve them I suggest overriding setProgress and adding onSizeChanged call to it.
我在将这段代码与 setProgress 方法一起使用时遇到问题。为了解决这些问题,我建议重写 setProgress 并添加 onSizeChanged 调用。此处添加了代码..
通过添加以下代码来执行选定的悬停操作:
1.setPressed(true);setSelected(true);//在 ACTION_DOWN 中添加此代码
2. setPressed(false);setSelected(false);//将其添加到 ACTION_UP 中
,并在 xml 中为选定的悬停选项编写代码。
这对我有用......
I had problem while using this code with setProgress method. To solve them I suggest overriding setProgress and adding onSizeChanged call to it.Added code here ..
selected hover actions are performed by adding the following code:
1.setPressed(true);setSelected(true);//Add this in ACTION_DOWN
2.setPressed(false);setSelected(false);//Add this in ACTION_UP
And Write code for selected hover options in ur xml.
This is working for me...
感谢 Paul Tsupikoff、Fatal1ty2787 和 Ramesh 提供了这段出色的代码。
就我个人而言,我想要一个与给定代码相比是颠倒的垂直滑块。换句话说,拇指越低,该值就会增加而不是减少。改变四行似乎已经解决了这个问题。
首先,我更改了 Paul 最初给出的
onDraw()
方法。rotate()
和translate()
调用现在具有以下参数:然后,我对
onTouchEvent( )
由 Fatal1ty2787 给出。对setProgress()
的调用现在如下所示:最后,对
onProgressChanged()
的调用如下所示:现在,如果 Google 能分享我们对此功能的兴趣就好了…… ..
Thanks to Paul Tsupikoff, Fatal1ty2787 and Ramesh for this excellent code.
Personally, I wanted a vertical slider that is upside-down compared to the given code. In other words, the value increases, rather than decreases, the lower the thumb. Changing four lines seems to have taken care of this.
First, I changed the
onDraw()
method as originally given by Paul. Therotate()
andtranslate()
calls now have these arguments:Then I made two changes to the
ACTION_MOVE
case inonTouchEvent()
as given by Fatal1ty2787. The call tosetProgress()
now looks like this:Finally, the call to
onProgressChanged()
looks like this:Now, if only Google shared our interest in this feature....
对于
API 11及更高版本
,可以使用seekbar的XML属性
(android:rotation="270"
)来实现垂直效果。对于较旧的 API 级别(例如 API10),请使用:
https://github.com/AndroSelva/Vertical-SeekBar-Android
For
API 11 and later
, can useseekbar's XML attributes
(android:rotation="270"
) for vertical effect.For older API level (ex API10),use:
https://github.com/AndroSelva/Vertical-SeekBar-Android
另一个想法可能是更改 MotionEvent 的 X 和 Y 坐标并将它们传递给超级实现:
在这种情况下,无需调用 setProgress(int) 方法,因此您可以使用布尔标志“fromUser”在 OnSeekBarChangeListener.onProgressChanged() 中确定查找是否由用户交互产生。
Another idea could be to change the X and Y coordinates of the MotionEvent and pass them to the super-implementation:
In this case it is not necessary to call the setProgress(int) method and therefore you could use the boolean-flag "fromUser" in OnSeekBarChangeListener.onProgressChanged() to determine if the seeking was produced by an user interaction.
根据 Paul Tsupikoff 的回答,这里是
AppCompatVerticalSeekBar
:在布局文件中使用它:
Based on Paul Tsupikoff's answer, here is the
AppCompatVerticalSeekBar
:Use it in your layout file:
目标平台
Android 2.3.x (Gingerbread) 的
到 Android 7.x (Nougat)
入门
该库发布在 jCenter 上。只需将这些行添加到 build.gradle 中即可。
用法
布局 XML
注意:Android N+ 需要
android:splitTrack="false"
。Java代码
Target platforms
from Android 2.3.x (Gingerbread)
to Android 7.x (Nougat)
Getting started
This library is published on jCenter. Just add these lines to build.gradle.
Usage
Layout XML
NOTE:
android:splitTrack="false"
is required for Android N+.Java code