seekbar 触碰时变更thumb
时间: 2023-08-02 18:09:42 浏览: 71
要实现当用户触碰SeekBar时,动态改变thumb的外观,你可以使用SeekBar的OnSeekBarChangeListener接口。具体实现步骤如下:
1. 在xml文件中设置SeekBar的thumb属性为自定义的Drawable,例如:
```xml
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:thumb="@drawable/seek_thumb" />
```
2. 在代码中获取SeekBar对象,并设置OnSeekBarChangeListener,例如:
```java
SeekBar seekBar = findViewById(R.id.seek_bar);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 当SeekBar的进度改变时,可以在这里动态改变thumb的外观
// 比如根据进度值设置不同的Drawable
if (progress < 50) {
seekBar.setThumb(getResources().getDrawable(R.drawable.thumb_1));
} else {
seekBar.setThumb(getResources().getDrawable(R.drawable.thumb_2));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// 用户开始触碰SeekBar时调用
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// 用户停止触碰SeekBar时调用
}
});
```
在onProgressChanged()方法中,根据SeekBar的进度值动态改变thumb的外观即可。注意,要根据不同的进度值设置不同的Drawable,否则thumb的外观会在拖动过程中一直改变,不符合预期效果。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)