简体   繁体   English

Android:自定义seekbar进度可绘制对象呈现不正确

[英]Android: Custom seekbar progress drawable is rendered incorrectly

I intend to make a custom seekbar, with my own set of images for the background and progress drawables. 我打算制作一个自定义的搜索栏,并为背景和进度可绘制对象设置自己的图像。 I've made 2 9-patch images, one each for the background and progress drawables. 我已经制作了2张9补丁图像,每张分别用于背景和进度可绘制对象。 Here is the code I am using for the progressDrawable: 这是我用于progressDrawable的代码:

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

    <item android:id="@+id/background"
        android:drawable="@drawable/experience_seekbar_background" />

    <item android:id="@+id/progress">
        <clip
            android:clipOrientation="horizontal"
            android:gravity="left|top"
            android:drawable="@drawable/experience_seekbar_progress" />
    </item>
</layer-list>

Also, here is the xml definition of my seekbar: 另外,这是我的seekbar的xml定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <SeekBar
        android:id="@+id/experienceSeekBar"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:max="2"
        android:progress="1"
        android:progressDrawable="@drawable/experience_seekbar" />
</LinearLayout>

The problem is, the progress portion of the drawable is drawn below the background portion, instead of directly overlapping it. 问题在于,可绘制对象的进度部分绘制在背景部分下方,而不是直接重叠在背景部分之下。

Expected/Desired: ( http://i.imgur.com/C27dQZK.png ) 预期/期望:( http://i.imgur.com/C27dQZK.png

Actual Output: ( http://i.imgur.com/ftNlMMI.png ) 实际输出:( http://i.imgur.com/ftNlMMI.png

Having gone through tens of custom seekbar tutorials, I am still stuck at what could I possibly be doing wrong here. 在经历了几十个自定义搜寻栏教程之后,我仍然停留在我可能在这里做错的事情上。 It might be something very trivial, but I am just not able to spot it. 这可能是非常琐碎的,但我只是无法发现它。

It turns out there were several things at play here. 事实证明,这里有几件事在起作用。

1) I used 9-patch images for the seekBar background and progress drawables. 1)我使用了9补丁图像作为seekBar背景和进度可绘制对象。 However, I did not know that 9-patch images also have, apart from the stretch lines at the top and left edges, padding lines at the right and bottom edges. 但是,我不知道除了顶部和左侧边缘的拉伸线之外,右侧和底部边缘的填充线也都具有9补丁图像。 These define the content area of the image, and the area left automatically becomes the padding area (ie the non-content area counts towards the padding of the view for which this image will be used as background). 这些定义了图像的内容区域,剩下的区域自动成为填充区域(即,非内容区域将计入该图像将用作背景的视图的填充)。 Also, if no padding lines are specified, android uses the stretch lines as padding lines. 另外,如果未指定填充线,则android将拉伸线用作填充线。

2) In a layer-list, the padding of items stacks up. 2)在层列表中,项目的填充会堆叠起来。 This means that if the first item in a layer-list has some padding, this padding will add up to the padding of all items after it, the second item's padding adds up to padding of all items below it, and so on. 这意味着,如果图层列表中的第一个项目具有某些填充,则该填充将加总其后所有项目的填充,第二个项目的填充将总计其下所有项目的填充,依此类推。

So in my case, the 9-patch image used as seekBar background (first item in the layer-list) got some unintended padding (because stretch lines were being used as padding lines), and the seekBar progress drawable got this padding added to its own unintended padding (again, since it was a 9-patch) which caused it to be displayed below the background. 因此,在我的情况下,用作seekBar背景的9补丁图像(图层列表中的第一项)得到了一些意外填充(因为将拉伸线用作了填充线),而seekBar进度可绘制对象将此填充添加到了它的填充中。自己的意外填充(再次,因为它是9补丁),导致其显示在背景下方。

To correct this, I changed the 9-patch images and set the padding lines to their full length ie I defined the entire image as the content area. 为了更正此问题,我更改了9补丁图像并将填充线设置为它们的全长,即我将整个图像定义为内容区域。 There was no other option, as any padding would lead to twice the padding for the progress drawable, displacing it from its intended position (which is exactly above the background drawable). 没有其他选择,因为任何填充都将导致可绘制进度的填充的两倍,将其从其预期位置(恰好在背景可绘制上方)移开。

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

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