简体   繁体   English

Android:SeekBar与自定义drawable

[英]Android: SeekBar with custom drawable

I have SeekBar with custom drawable. 我有自定义绘图的SeekBar。 Progress element is not rendered from left side exactly but it is moved to right. Progress元素不是从左侧精确渲染,而是向右移动。 How can I avoid this? 我怎么能避免这个?

在此输入图像描述

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@android:id/summary"
    android:background="@null"

    android:progressDrawable="@drawable/seekbar"
    android:thumb="@drawable/seekbar_thumb"/>

seekbar.xml seekbar.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/seekbar_background"/>
    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/seekbar_progress" />
    </item>

</layer-list>

seekbar_background.xml seekbar_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">

    <stroke android:width="2dp" android:color="@color/colorNeutral" />

</shape>

seekbar_progress.xml seekbar_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">

    <stroke
        android:width="4dp"
        android:color="@color/colorAccentDark" />
</shape>

EDIT: 编辑:

As mentioned in comments if there will be both widths same problem disappear. 如评论中所述,如果两个宽度相同,问题就会消失。 But I need make it with two different widths through drawable is it possible? 但我需要通过drawable使它有两种不同的宽度才有可能吗? Maybe with different shapes not just lines? 也许不同的形状不仅仅是线条?

I can not explain why this is an issue when drawing strokes, I think it has something to do with the strokes width, but I did not yet find the source to verify. 我无法解释为什么这是绘制笔画时的问题,我认为它与笔画宽度有关,但我还没有找到要验证的来源。

Fixing the Overlay 修复叠加层

To remove the issue at hand, you can just set an inset on your left side. 要解决手头的问题,您只需在左侧设置inset A value of 1dp or 2dp both work and the seekbar will be drawn properly. 值为1dp2dp都可以正常工作,并且会正确绘制搜索条。

Note: Using this approach there should be no risk that the background could be too short and not visible with low progress values, since the thumb would overlay and hide it in any case. 注意:使用这种方法不应该存在背景太短而且进度值低的情况下不可见的风险,因为在任何情况下拇指都会覆盖并隐藏它。

<?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/seekbar_background"
        android:left="2dp"><!-- or 1dp -->
    </item>
    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/seekbar_progress"/>
    </item>
</layer-list>

Observation 意见

When you create a line using shape drawable it leaves the left and right padding (from the view in which this drawable is being used.)equal to the half of its stroke width. 当您使用shape drawable创建一条线时,它会留下左右填充(从使用此drawable的视图中)。等于其笔触宽度的一半。

Workaround 解决方法

You need to wrap your progress_background with an inset drawable and then set it as progress background. 您需要使用插入drawable包装progress_background,然后将其设置为进度背景。 The inset can be like the following- 插图可以如下 -

inset_seekbar_background.xml inset_seekbar_background.xml

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/seekbar_progress"
    android:insetLeft="2dp"
    android:insetRight="2dp" />

and your final seekbar.xml will be like- 你的最终seekbar.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/inset_seekbar_background"/>
    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/seekbar_progress" />
    </item>

</layer-list>

Alternates 候补

1.You can use an 9 path image for line as it is done in the default material design- 1.您可以使用9路径图像作为线路,因为它是在默认材料设计中完成的 -

<?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/abc_scrubber_primary_mtrl_alpha" />
    <item android:id="@android:id/secondaryProgress">
        <scale android:scaleWidth="100%">
            <selector>
                <item android:state_enabled="false">
                    <color android:color="@android:color/transparent" />
                </item>
                <item android:drawable="@drawable/abc_scrubber_primary_mtrl_alpha" />
            </selector>
        </scale>
    </item>
    <item android:id="@android:id/progress">
        <scale android:scaleWidth="100%">
            <selector>
                <item android:state_enabled="true">
                    <color android:color="@android:color/transparent" />
                </item>
                <item android:drawable="@drawable/abc_scrubber_primary_mtrl_alpha" />
            </selector>
        </scale>
    </item>
</layer-list><!-- From: file:/usr/local/google/buildbot/src/googleplex-android/mnc-supportlib-release/frameworks/support/v7/appcompat/res/drawable/abc_seekbar_track_material.xml -->

在此输入图像描述 在此输入图像描述 在此输入图像描述 在此输入图像描述

2.You can set the theme to your view with different color in android style as- 2.您可以在Android风格中使用不同颜色将主题设置为您的视图 -

<style name="AppTheme.SeekBar">
        <item name="colorPrimary">@color/colorAccent</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorPrimaryDark</item>
</style>

Here I have used all there options for Seekbars- 在这里,我使用了Seekbars的所有选项 -

            <SeekBar 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme.SeekBar" />

            <SeekBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <SeekBar
                android:layerType="software"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:progressDrawable="@drawable/seekbar" />

And here is the output- 这是输出 - 在此输入图像描述

As it appear the issue in progress I think they both should be the same width 由于它似乎正在进行中的问题,我认为它们都应该是相同的宽度

but you in progress make width = 4 android:width="4dp" 但你正在进行宽度= 4 android:width="4dp"

and seekbar width = 2 android:width="2dp" 和seekbar width = 2 android:width="2dp"

just match them to be 2 or 4 dont use different values 只是将它们匹配为2或4不要使用不同的值

Try using a .9.png drawable of width 4dp (converted to px) . 尝试使用宽度为4dp的.9.png drawable(转换为px)。

Keep 1 dp at top and bottom transparent. 保持顶部和底部的1 dp透明。

And add colour to the center 2 dp of your choice for the background. 并为您选择的背景中心2 dp添加颜色。

This way both background and progress will be of same width but the background will look thinner than progress bar. 这样,背景和进度将具有相同的宽度,但背景看起来比进度条更薄。

Try like this using android:layout_marginLeft="-3dp" or -2dp 尝试使用android:layout_marginLeft="-3dp"-2dp

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@android:id/summary"
    android:background="@null"
    android:layout_marginLeft="-2dp"
    android:progressDrawable="@drawable/seekbar"
    android:thumb="@drawable/seekbar_thumb"/>`

If your choices of color has a limit and is predefined. 如果您的颜色选择有限制并且是预定义的。 You can try to add XML files with Seekbars using different themes. 您可以尝试使用不同的主题添加带有Seekbars的XML文件。

Something like this: 像这样的东西:

themed_seekbar_one.xml themed_seekbar_one.xml

<?xml version="1.0" encoding="utf-8"?>
<SeekBar
    ...
    android:theme="@style/first_theme">
</SeekBar>

themed_seekbar_two.xml themed_seekbar_two.xml

<?xml version="1.0" encoding="utf-8"?>
<SeekBar
    ...
    android:theme="@style/second_theme">
</SeekBar>

You can then inflate does views programatically like this: 然后,您可以以编程方式对视图进行充气:

SeekBar seekbar = (SeekBar)getLayoutInflater().inflate(R.layout.themed_seekbar_xxx, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM