这篇文章主要为大家详细介绍了Android中SeekBar拖动条使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Android中SeekBar拖动条使用方法的具体代码,供大家参考,具体内容如下
SeekBar控件效果展示

拖动条SeekBar继承了ProgressBar,因此ProgressBar所支持的xml属性和方法完全适合SeekBar。只是进度条ProgressBar采用颜色填充来表明进度完成程度,拖动条SeekBar则通过滑块的外置来标识——拖动滑块允许进度值的改变。(例如:条件Android系统的音量)
如上图,通过拖动SeekBar滑块,实现图片透明度的修改。实现代码如下:
创建xml布局文件(activity_seek_bar.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SeekBarActivity">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/pineapple" />
<!--android:thumb 自定义一个Drawable对象(设置滑块的小图标)-->
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="250"
android:progress="150"
android:thumb="@drawable/test" />
</LinearLayout>
滑块最大值为250,当前值为150。可通过拖动滑块进行改变。android:thumb 为滑块自定义一个Drawable对象(设置滑块的小图标),使滑块更加好看。
创建Activity操作实现类:
public class SeekBarActivity extends AppCompatActivity {
private ImageView imageView;//图片
private SeekBar seekBar;//拖动条
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seek_bar);
imageView = (ImageView)findViewById(R.id.image);
seekBar = (SeekBar)findViewById(R.id.seekbar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {、
//滑块位置变动时触发该方法
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
//设置图片透明度
imageView.setImageAlpha(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
SeekBar滑块位置变动时,ImageVIew的透明度将变为该拖动条SeekBar的当前值,将看到顶部图片展示的效果。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
本文标题为:Android中SeekBar拖动条使用方法详解
基础教程推荐
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- Flutter手势密码的实现示例(附demo) 2023-04-11
- Android多返回栈技术 2023-04-15
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- Android中的webview监听每次URL变化实例 2023-01-23
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- android studio按钮监听的5种方法实例详解 2023-01-12
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
