今天要改一个播放器的UI,改到下方的播放进度条的时候,本来打算自定义一个来着,但是看着界面也不复杂,就想着用系统提供的控件来试试,主要就是seekBar的样式,进度的颜色是渐变的,进度的滑块是个白色圆形。下面主要讲的是系统控件的各个属性,许多时候仅仅依靠系统控件就可以实现好多效果了:
<SeekBar
android:id="@+id/progressBar1"
android:layout_width="match_parent"
android:layout_height="20dp"
android:progress="50"
android:paddingLeft="5dp"
android:paddingRight="5dp"
<!-- 通过这个属性来给seekBar滑块换图片 -->
android:thumb="@drawable/kids_play_progress_thumb"
android:thumbOffset="0dp"
<!-- 通过这个属性来给seekBar来设置背景色,进度条颜色以及二级进度颜色 -->
android:progressDrawable="@drawable/kids_play_progress" />
主要来讲讲android:progressDrawable这个属性,在drawable文件夹内新建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="@drawable/background">
</item>
<item android:id="@android:id/progress" android:drawable="@drawable/progress">
</item>
</layer-list>
这样就可以实现效果了,但是有个问题,就是滑块和进度条的颜色是一样高度的,效果图是要突出滑块的,也就是滑块要比进度条上下突出一点,这个时候就要引出今天的主角了InsetDrawable,在上方的item中可以用标签,inset标签有这些属性,如果用的图片,在drawable中设置就OK,如果用shape就在首尾标签内加上shape,
android:drawable
android:insetBottom
android:insetLeft
android:insetRight
android:insetTop
android:visible
修改后的layer-list如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<inset
android:insetBottom="5dp"
android:insetTop="5dp" >
<shape>
<gradient
android:angle="0"
android:endColor="#99ffffff"
android:startColor="#99ffffff" />
<stroke android:color="#99ffffff" android:width="1px" />
</shape>
</inset>
</item>
<item android:id="@android:id/progress">
<clip>
<inset
android:insetBottom="5dp"
android:insetTop="5dp" >
<shape>
<gradient
android:angle="0"
android:endColor="#8eabff"
android:startColor="#b5c8ff" />
<stroke android:color="#8eabff" android:width="1px" />
</shape>
</inset>
</clip>
</item>
</layer-list>
这个inset标签其实就是来设置这个图片内缩,只是内容内缩,这样可以实现进度条比滑块窄。
本文介绍如何使用Android系统控件SeekBar及自定义属性实现渐变色进度条和突出滑块的效果,包括使用InsetDrawable来调整滑块与进度条的高度差异。
346

被折叠的 条评论
为什么被折叠?



