一、简介

SeekBar是进度条。我们使用进度条时,可以使用系统默认的进度条;也可以自定义进度条的图片和滑块图片等


二、创建一个activity,包含2个SeekBar。
第1个SeekBar是系统默认的SeekBar。
第2个SeekBar是自定义SeekBar,使用自定义的背景图和滑块图片。


应用层代码

package com.seekbartest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
    private static final String TAG = "ruanxingzi";

    // 与“系统默认SeekBar”对应的TextView
    private TextView mTvDef;
    // 与“自定义SeekBar”对应的TextView
    private TextView mTvSelf;
    // “系统默认SeekBar”
    private SeekBar mSeekBarDef;
    // “自定义SeekBar”
    private SeekBar mSeekBarSelf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        // 与“系统默认SeekBar”对应的TextView
        mTvDef = (TextView) findViewById(R.id.tv_def);
        // “系统默认SeekBar”
        mTvSelf = (TextView) findViewById( R.id.tv_self );

        mSeekBarDef = (SeekBar) findViewById( R.id.seekbar_def );
        mSeekBarSelf = (SeekBar) findViewById( R.id.seekbar_self );
        mSeekBarSelf.setOnSeekBarChangeListener( this );
    }

    /*
     * SeekBar滚动时的回调函数
     */

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        Log.d(TAG, "seekid:"+seekBar.getId()+", progess"+progress);
        switch(seekBar.getId()) {
            case R.id.seekbar_def:{
                // 设置“与系统默认SeekBar对应的TextView”的值
                mTvDef.setText(getResources().getString(R.string.title)+" : "+String.valueOf(seekBar.getProgress()));
                break;
            }
            case R.id.seekbar_self: {
                // 设置“与自定义SeekBar对应的TextView”的值
                mTvSelf.setText(getResources().getString(R.string.title1)+" : "+String.valueOf(seekBar.getProgress()));
                break;
            }
            default:
                break;
        }
    }

    /*
    * SeekBar开始滚动的回调函数  按住seekbar时的事件监听处理
    */
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    /*
    * SeekBar停止滚动的回调函数  放开seekbar时的时间监听处理 
    */
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}


代码说明:


要监听SeekBar的滑动消息,通过实现“SeekBar.OnSeekBarChangeListener”接口。这个接口中包含3个方法onStartTrackingTouch()、onStopTrackingTouch()和onProgressChanged()。


layout文件

<?xml version="1.0" encoding="utf-8"?>
<!--
 android:max="":设置最大进度
 android:progress="":设置当前进度
 android:secondaryProgress="":设置第二进度
 android:indeterminate="":设置是否为不明确进度进度条
 android:thumb="":设置seekbar滑动快的图片
 android:progressDrawable="":进度条背景图片
 android:thumbTint="":滑动块颜色,API21以上才有效
 -->
<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="com.seekbartest.MainActivity">

    <TextView
        android:id="@+id/tv_def"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title"
        />

    <!--
       max=100,代表它的取值范围是0-100,共101个值;
       progress=10,代表默认值是10
   -->
    <SeekBar
        android:id="@+id/seekbar_def"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="10"
        />

    <TextView
        android:id="@+id/tv_self"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title1"
        />

    <!--
    max=100,代表它的取值范围是0-100,共101个值;
    progress=20,代表默认值是20
    progressDrawable,表示SeekBar的背景图片
    thumbe,表示SeekBar的滑块图片
    自定义SeekBar的背景定义为:android:progressDrawable="@drawable/bg_bar"。
    自定义SeekBar的滑块定义为:android:thumb="@drawable/thumb_bar"。
    -->

    <SeekBar
        android:id="@+id/seekbar_self"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="20"
        android:progressDrawable="@drawable/bg_bar"
        android:thumb="@drawable/thumb_bar"
        android:layout_marginTop="20dp"
        />

</LinearLayout>


自定义SeekBar的背景定义为:android:progressDrawable="@drawable/bg_bar"。


它调用的bg_bar.xml的内容如下:


<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 背景图 -->
    <item android:id="@android:id/background" android:drawable="@mipmap/bar_dn" />
    <!-- 第二进度图 -->
    <item android:id="@android:id/secondaryProgress" android:drawable="@mipmap/bar_dn" />
    <!-- 进度度 -->
    <item android:id="@android:id/progress" android:drawable="@mipmap/bar_up" />
</layer-list>


自定义SeekBar的滑块定义为:android:thumb="@drawable/thumb_bar"。


它调用的thumb_bar.xml的内容如下:


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 按下状态 -->
    <item android:state_pressed="true"
          android:drawable="@mipmap/thumb_dn" />

    <!-- 焦点状态 -->
    <item android:state_focused="true"
          android:drawable="@mipmap/thumb_up" />

    <!-- 默认状态 -->
    <item android:drawable="@mipmap/thumb_up" />
</selector>