SeekBar通过设置ProgressDrawable,可以自主定义显示样式。常规使用方式通过xml布局方式实现,但是在sdk中不能有xml布局,或者动态生成的SeekBar控件用xml布局实现不能正常显示,这时SeekBar就只能使用纯代码实现。
常规用layer-list 的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">
<shape>
<solid android:color="#cc999999" />
<size android:height="3dp" />
<corners android:radius="1.5dip" />
<stroke android:color="#00FFFFFF" android:width="18dp"/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="#9f4ef1" />
<size android:height="3dp" />
<corners android:radius="1.5dip" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="1.5dip" />
<!--<solid android:color="#0add2c" />-->
<gradient
android:angle="0"
android:endColor= "#0886D6"
android:startColor="#0886D6"/>
<size android:height="3dp" />
<stroke android:color="#00FFFFFF" android:width="18dp"/>
</shape>
</clip>
</item>
</layer-list>
上面是正常的xml布局样式,这样的布局我们需要用java纯代码实现,如下:
public static Drawable genSeekProgressDrawable(Context context) {
Drawable[] drawables = new Drawable[3];
GradientDrawable roundRect = new GradientDrawable();
roundRect.setShape(GradientDrawable.RECTANGLE);
roundRect.setColor(Color.parseColor(ViewUtils.getInstance().progressColor1));
roundRect.setSize(1,FormatUtils.dip2px(1,context));
roundRect.setCornerRadius(FormatUtils.dip2px(1.5f,context));
//roundRect.setColor(Color.WHITE);
//roundRect.setSize(1, 10);
//roundRect.setCornerRadius(50);
drawables[0] = roundRect;
roundRect = new GradientDrawable();
roundRect.setShape(GradientDrawable.RECTANGLE);
roundRect.setColor(Color.parseColor(ViewUtils.getInstance().progressColor3));
roundRect.setSize(1,FormatUtils.dip2px(1,context));
roundRect.setCornerRadius( FormatUtils.dip2px(1.5f,context));
ClipDrawable clipDrawable = new ClipDrawable(roundRect, Gravity.LEFT, VERTICAL);
drawables[1] =clipDrawable;
roundRect = new GradientDrawable();
roundRect.setShape(GradientDrawable.RECTANGLE);
roundRect.setColor(Color.parseColor(ViewUtils.getInstance().colorProgress));
roundRect.setSize(1,FormatUtils.dip2px(1,context));
roundRect.setCornerRadius(FormatUtils.dip2px(1.5f,context));
clipDrawable = new ClipDrawable(roundRect, Gravity.LEFT, VERTICAL);
drawables[2] = clipDrawable;
LayerDrawable layerDrawable = new LayerDrawable(drawables);
layerDrawable.setId(0,android.R.id.background);
layerDrawable.setId(1,android.R.id.secondaryProgress);
layerDrawable.setId(2,android.R.id.progress);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
layerDrawable.setLayerHeight(0,FormatUtils.dip2px(2,context));
layerDrawable.setLayerHeight(1,FormatUtils.dip2px(2,context));
layerDrawable.setLayerHeight(2,FormatUtils.dip2px(2,context));
layerDrawable.setLayerGravity(0,Gravity.CENTER_VERTICAL);
layerDrawable.setLayerGravity(1,Gravity.CENTER_VERTICAL);
layerDrawable.setLayerGravity(2,Gravity.CENTER_VERTICAL);
}
return layerDrawable;
}
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
最后设置seekBar.setProgressDrawable即可;
对于6.0以上手机需要加上最后if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)的判断设置,否则如果你SeekBar设置seekBar.setThumb( bitmapDrawable);
那么SeekBar的进度条高度也会变得和Thumb一样高,会撑满。
对于6.0以下手机可以用反射实现:
try {
Class<?> superclass = seekBar.getClass().getSuperclass().getSuperclass();
Field mMaxHeight = superclass.getDeclaredField("mMaxHeight");
mMaxHeight.setAccessible(true);
mMaxHeight.set(seekBar,FormatUtils.dip2px(2,context));
} catch (Exception e) {
e.printStackTrace();
}