SeekBar 拖动进度条控件

SeekBar是一个进度条控件,大概如下图

可以进行自定义,分两个部分,分别是背景和滑片

首先是背景

一般需要有颜色的变化效果,所以通过样式来定义,因为里面都是shap,所以可以自定义圆角一类的,文件名称seekbar_bg

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--background代表背景样式-->
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="#ff51495e"/>
        </shape>
    </item>
    <!--secondaryProgress代表缓冲样式-->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="#f9062a"/>
            </shape>
        </clip>
    </item>
    <!--progress代表进度条样式-->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#2db334"/>
            </shape>
        </clip>
    </item>
</layer-list>

使用的时候,直接引用这个样式就可以了

<SeekBar
    android:layout_margin="100px"
    android:thumb="@mipmap/thumb"
    android:maxHeight="20px"
    android:progressDrawable="@drawable/seekbar_bg"
    android:layout_width="400px"
    android:layout_height="60px" />

划片,可以选择图片,也可以选择自定义样式,是thumb这个标签

如果选择图片,需要注意的是,一定要写thumbOffset="0dp",否则图片划片到头和尾的时候会显示不全

<SeekBar
    android:thumbOffset="0dp"
    android:progress="0"
    android:layout_margin="100px"
    android:thumb="@mipmap/thumb"
    android:maxHeight="20px"
    android:progressDrawable="@drawable/seekbar_bg"
    android:layout_width="400px"
    android:layout_height="60px" />

划片如果选择自定义,文件名:seekbar_thumb_bg

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--获取焦点和没有按下的时候-->
    <item android:drawable="@drawable/seekbar_thumb_normal" android:state_focused="true" android:state_pressed="false"/>
    <!--获取焦点但按下的时候-->
    <item android:drawable="@drawable/seekbar_thumb_pressed" android:state_focused="true" android:state_pressed="true"/>
    <!--没有获取焦点按下的时候-->
    <item android:drawable="@drawable/seekbar_thumb_pressed" android:state_focused="false" android:state_pressed="true"/>
    <!--默认的时候-->
    <item android:drawable="@drawable/seekbar_thumb_normal"/>
</selector>

使用,progress 代表默认的值

<SeekBar
    android:progress="0"
    android:layout_margin="100px"
    android:thumb="@drawable/seekbar_thumb_bg"
    android:maxHeight="20px"
    android:progressDrawable="@drawable/seekbar_bg"
    android:layout_width="400px"
    android:layout_height="60px" />

常用方法,其实就是监听

SeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        //数值改变调用
    }


    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        //开始拖动调用
    }


    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        //停止拖动调用
    }
});

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值