Android 自定义SeekBar以及几个要注意的问题

//SeekBar的定义
<?xml version="1.0" encoding="utf-8"?>
<SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/seekbar_toumingdu"
    android:thumb="@drawable/seekbar_thumb_bigoval"
    android:progressDrawable="@drawable/seekbar_progress"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:thumbOffset="15dp"
    android:maxHeight="5dp"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
   >
</SeekBar>

thumb定义上面的滑块的drawable实现,progressDrawable定义下面长条的实现。

thumb使用shapDrawable定义为一个圆形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <size android:height="30dp"
        android:width="30dp"/>
    <solid android:color="#8003a9f4"/>
</shape>

progressDrawable定义:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background"
        >
        <shape>
            <gradient
                android:angle="270"
                android:centerColor="#ffffff"
                android:centerY="0.45"
                android:endColor="#ffffff"
                android:startColor="#d4d4d4" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
               <solid android:color="#03a9f4"/>
            </shape>
        </clip>
    </item>
</layer-list>
子项目item的id指定相应的drawable实现,使用item的drawable属性直接指定好像有点问题。

有background,progress和secondrayProgress三个,选择其中需要的的即可。


几个需要注意的问题:

(1)自定义的滑块滑动到边缘时要是中心处于边缘位置,那么会有一部分滑块超出长条边缘,这时要指定paddingleft和paddingright,不然不能显示超出部分的滑块。

(2)滑块滑到边缘时,还有可能中心位置并不是恰好处于边缘,这时候要指定thumbOffset属性,长度为滑块宽的一半,那样就能显示好了。

(3)长条宽度是通过maxHeight指定,使用其它方法指定宽度不行,不影响滑块的。


想要实现更炫的滑块,使用渐变的shap,layerDrawable等,不知道为什么用到手机这些效果都不行了,不知道有没有人懂。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值