Android 纯代码实现Seekbar布局,解决maxHeight设置问题和缓冲进度条样式问题

Seekbar常规使用方式通过xml布局方式实现,但是由于我们的是sdk,不能有xml布局,所以SeekBar使用纯代码实现。但是这样就遇到了很多问题。
首先是SeekBar设置setProgressDrawable问题。因为我们的是视频播放器,所以这个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" />
        </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>
                <solid android:color="#0add2c" />
                <size android:height="3dp" />
                <corners android:radius="1.5dip" />
            </shape>
        </clip>
    </item>
</layer-list>

这是正常的xml布局样式,这样的布局我们需要用java纯代码实现,如下:

 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));
        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);

然后直接SeekBar.setProgressDrawable就可以。

第二个问题是,SeekBar高度问题,这个才是最坑的,研究了很多,SeekBar如果用xml布局会有两个方法android:maxHeight="4dp"android:minHeight="4dp",但是如果是java写,就只有progressBar.setMinimumHeight(FormatUtils.dip2px(1,context));,用来设置最小高度,这样的话就有一个问题,如果你SeekBar设置seekBar.setThumb( bitmapDrawable);哪么SeekBar的进度条高度也会变得和Thumb一样高,会撑满。这里有两个解决办法:
一是在6.0以上的手机适用,就是在上面设置layerDrawable代码后面再加两句

 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);
        }

整体是这样:

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));
        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;
    }

二是适配所有的手机,这样的话我没有找到好的方法,只能用反射去修改SeekBar的父类的父类progressBar的最大高度,mMaxHeight的值,这个就是设置SeekBar的最大高度的值,xml代码android:maxHeight="4dp"其实改变的就是这个的值。
代码也很简单:

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();
            }

其实最好的方法就是两个一起用,6.0以下用反射,6.0以上用layerDrawable的方法去实现。这是我个人研究的办法,网上也没有实现的办法,找了很久。如果有更好的求告知啊!!!!!

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值