Android 纯代码实现Seekbar布局,改变显示样式

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();
            }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值