在 Android 中实现滑块 (SeekBar)
我想实现一个滑块,它基本上是两条线,一根垂直,一根水平,穿过触摸屏幕的位置。我已经成功制作了一个,但我遇到了问题:
- 滑块不是很平滑,移动手指时有轻微的延迟
- 如果我放置两个滑块,它就不是多点触控,并且我想同时使用这两个滑块它们同时进行
这是代码:
public class Slider extends View {
private Controller controller = new Controller();
private boolean initialisedSlider;
private int sliderWidth, sliderHeight;
private Point pointStart;
private Paint white;
private int mode;
final static int VERTICAL = 0, HORIZONTAL = 1, BOTH = 2;
public Slider(Context context) {
super(context);
setFocusable(true);
// TODO Auto-generated constructor stub
}
public Slider(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
pointStart = new Point();
initialisedSlider = false;
mode = Slider.BOTH;
}
@Override
protected void onDraw(Canvas canvas) {
if(!initialisedSlider) {
initialisedSlider = true;
sliderWidth = getMeasuredWidth();
sliderHeight = getMeasuredHeight();
pointStart.x = (int)(sliderWidth/2.0);
pointStart.y = (int)(sliderHeight/2.0);
controller = new Controller(pointStart, 3);
white = new Paint();
white.setColor(0xFFFFFFFF);
}
canvas.drawLine(controller.getCoordX(),0,
controller.getCoordX(),sliderHeight,
white);
canvas.drawLine(0, controller.getCoordY(),
sliderWidth, controller.getCoordY(),
white);
}
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
if(isInBounds(X,Y)) {
updateController(X, Y);
}
break;
case MotionEvent.ACTION_MOVE:
if(isInBounds(X,Y)) {
updateController(X, Y);
}
break;
case MotionEvent.ACTION_UP:
if(isInBounds(X,Y)) {
updateController(X, Y);
}
break;
}
invalidate();
return true;
}
private boolean isInBounds(int x, int y) {
return ((x<=(sliderWidth)) && (x>=(0))
&& (y<=(sliderHeight)) && (y>=(0)));
}
private void updateController(int x, int y) {
switch(mode) {
case Slider.HORIZONTAL:
controller.setCoordX(x);
break;
case Slider.VERTICAL:
controller.setCoordY(y);
break;
case Slider.BOTH:
controller.setCoordX(x);
controller.setCoordY(y);
break;
}
}
private class Controller {
private int coordX, coordY;
Controller() {
}
Controller(Point point, int width) {
setCoordX(point.x);
setCoordY(point.y);
}
public void setCoordX(int coordX) {
this.coordX = coordX;
}
public int getCoordX() {
return coordX;
}
public void setCoordY(int coordY) {
this.coordY = coordY;
}
public int getCoordY() {
return coordY;
}
}
}
和 XML 文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<com.android.lasttest.Slider
android:id="@+id/slider"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"/>
<com.android.lasttest.Slider
android:id="@+id/slider"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"/>
<com.android.lasttest.Slider
android:id="@+id/slider"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"/>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如何实现 SeekBar
将 SeekBar 添加到布局中
注意
max
是搜索栏可以达到的最高值。默认值为100
。最小值为0
。 xmlmin
值仅在 API 26 中可用,但您可以通过编程方式将0-100
范围转换为早期版本所需的任何值。progress
是滑块点(称为“拇指”)的初始位置。android:rotation="270"
。侦听代码中的更改注释
如果
onStopTrackingTouch
中更新 UI 即可。另请参阅
How to implement a SeekBar
Add the SeekBar to your layout
Notes
max
is the highest value that the seek bar can go to. The default is100
. The minimum is0
. The xmlmin
value is only available from API 26, but you can just programmatically convert the0-100
range to whatever you need for earlier versions.progress
is the initial position of the slider dot (called a "thumb").android:rotation="270"
.Listen for changes in code
Notes
onStopTrackingTouch
.See also
Android 提供水平滑块
并实现 OnSeekBarChangeListener
如果您想要垂直 Seekbar,请按照此链接
Android provides slider which is horizontal
and implement OnSeekBarChangeListener
If you want vertical Seekbar then follow this link
对于未来的读者!
从 材质组件 android 1.2.0-alpha01< /a>,您有
slider
组件,例如:
For future readers!
Starting from material components android 1.2.0-alpha01, you have
slider
componentex: