如何为SeekBar背景创建图层列表drawable?

时间:2017-12-14 13:44:08

标签: android drawable shapes layer-list

我设法创建了一个xml drawable,看起来像这样,它应该是这样的:

drawable

代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="line">
            <stroke android:color="@color/black"
                android:width="3dp" />
            <solid android:color="#f000"/>
        </shape>
    </item>

    <item android:top="1dp" android:left="360dp" android:bottom="1dp" android:right="0dp">
        <shape android:shape="oval">
            <size android:width="15dp"
                android:height="15dp"/>
            <solid android:color="@color/black"/>
        </shape>
    </item>

    <item android:top="1dp" android:left="246dp" android:bottom="1dp" android:right="114dp">
        <shape android:shape="oval">
            <size android:width="25dp"
                android:height="25dp"/>
            <solid android:color="@color/black"/>
        </shape>
    </item>

    <item android:top="1dp" android:left="114dp" android:bottom="1dp" android:right="246dp">
        <shape android:shape="oval">
            <size android:width="25dp"
                android:height="25dp"/>
            <solid android:color="@color/black"/>
        </shape>
    </item>

    <item android:top="1dp" android:left="0dp" android:bottom="1dp" android:right="360dp">
        <shape android:shape="oval">
            <size android:width="25dp"
                android:height="25dp"/>
            <solid android:color="@color/black"/>
        </shape>
    </item>

</layer-list>

然后我将它应用到我的SeekBar:

<SeekBar
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:progressDrawable="@drawable/seekbar_style"
/>

但它不起作用......我得到这个:

not working

所以现在我尝试以编程方式进行,因为它可能是左,上,右,下DP的问题....这是代码,但结果不是我期待的......

int width = seekBar.getMeasuredWidth();

ShapeDrawable line = new ShapeDrawable();
line.getPaint().setStrokeWidth(3);
line.getPaint().setColor(Color.BLACK);

ShapeDrawable bubble1 = new ShapeDrawable(new OvalShape());
bubble1.setBounds(width, 1, 0, 1);
bubble1.getPaint().setColor(Color.WHITE);
bubble1.setIntrinsicHeight(25);
bubble1.setIntrinsicWidth(25);

//bubble2, bubble3, bubble4 with different colors and then:

Drawable[] layers = {line, bubble1, bubble2, bubble3, bubble4};
seekBar.setProgressDrawable(new LayerDrawable(layers));

not working again

有人能指出我正确的方向吗?我做错了什么?

2 个答案:

答案 0 :(得分:1)

我不认为单独使用图层列表会让你随处可见。如果您不想创建自己的自定义搜索栏,我会在名称中检查一些带有SeekBar的自定义库,例如:

  1. https://github.com/channguyen/range-slider-view
  2. https://github.com/warkiz/IndicatorSeekBar
  3. https://github.com/alshell7/terminal-seekbar

答案 1 :(得分:0)

请尝试使用这个开源的GitHub存储库,它提供了您想要的解决方案,

Timeline-View

您的结果如下所示

<强>截图:

enter image description here

关于这个库的另一件事,你可以修改圆形颜色,样式,形状。