使用SeekBar制作可拖动的进度条

来源:互联网 发布:windows 3d主题包 编辑:程序博客网 时间:2024/05/04 04:18

SeekBar可以通过滑块的位置来标识数值,而且拖动条允许用户拖动滑块来改变进度值的大小


setMax----

setProgress----

setSecondaryProgress----设置SeekBar的第二数值


实现SeekVar.OnSeekBarChangeListener接口

数值改变(onProgressChanged)

开始拖动(onStartTrackingTouch)

停止拖动(onStopTrackingTouch)


main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <SeekBar        android:id="@+id/seekBar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:max="100"        android:progress="50" />    <TextView        android:id="@+id/tv1"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/tv2"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>


MainActivity.java

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {    private SeekBar seekBar;    private TextView tv1;    private TextView tv2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        seekBar = (SeekBar) findViewById(R.id.seekBar);        tv1 = (TextView) findViewById(R.id.tv1);        tv2 = (TextView) findViewById(R.id.tv2);        seekBar.setOnSeekBarChangeListener(this);    }    //数值改变    @Override    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {        tv1.setText("正在拖动");        tv2.setText("当前数值 : " + progress);    }    //开始拖动    @Override    public void onStartTrackingTouch(SeekBar seekBar) {        tv1.setText("开始拖动");    }    //停止拖动    @Override    public void onStopTrackingTouch(SeekBar seekBar) {        tv1.setText("停止拖动");    }}


自定义SeekBar进度条


android:progressDrawable="@drawable/seekbar_img" (改变进度条的样式)


android:thumb="@drawable/thumb" (改变滑块的样式)


my_thumb.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/select" android:state_pressed="true" android:state_window_focused="true" />    <item android:drawable="@drawable/select" android:state_focused="true" android:state_window_focused="true" />    <item android:drawable="@drawable/select" android:state_selected="true" android:state_window_focused="true" />    <item android:drawable="@drawable/normal" /></selector>

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <SeekBar        android:thumb="@drawable/my_thumb"        android:id="@+id/seekBar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:max="100"        android:progress="50" />    <TextView        android:id="@+id/tv1"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/tv2"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>



https://github.com/LiuchangDuan/demo





0 0
原创粉丝点击