如何在“Android水平进度条”中绘制垂直线

4

我需要制作一个类似下方图片的自定义横向进度条。

所需自定义横向进度条

我已经使用drawable中的layer-list设计了如下图片的进度条:

我的进度条

当进度大于25时,我想绘制一条垂直线,就像需要的图片那样。我写了以下代码但它没有起作用。

myProgressBar = (ProgressBar) findViewById(R.id.progress);
Canvas canvas = new Canvas();
Paint p = new Paint();
p.setStrokeWidth(2);
p.setColor(Color.WHITE);
canvas.drawLine(a, b, c, d, p);

if(myProgressBar.getProgress() > 25){
    myProgressBar.draw(canvas);
}

所以,请帮助我在进度条中画一条竖线。这是我在堆栈溢出中的第一个问题,所以可能会有一些错误,对此感到抱歉。

3个回答

1
你可以尝试使用图层列表。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@android:id/background">
       <shape>
            <corners android:radius="3dip" />
            <gradient
                android:angle="0"
                android:endColor="#e9e9f4"
                android:startColor="#e9e9f4" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:angle="0"
                    android:endColor="#16a023"
                    android:startColor="#16a023" />
            </shape>
        </clip>
    </item>
    <item android:id="@+id/line" >
        <rotate android:fromDegrees="90" android:pivotX="10%" >
           <shape android:shape="line" >
               <stroke android:width="2dp" android:color="#ffffff" />
           </shape>
       </rotate>
   </item>
</layer-list>

0

从我的角度来看,当该行大于25时,您必须呈现该行。尝试一下,然后让我知道。

if(myProgressBar.getProgress() > 25){
    canvas.drawLine(a, b, c, d, p);
    myProgressBar.draw(canvas);
}

0

这个问题很旧,但在谷歌上仍然出现。我不确定您想要进度状态上方还是下方的垂直线。Ali的答案使线条出现在上方,而我希望线条出现在下方,所以这是我的layer-list版本:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/light_gray"/>
                    <corners
                        android:bottomRightRadius="10dp"
                        android:topRightRadius="10dp" />
                </shape>
            </item>
            <item>
                <rotate android:fromDegrees="90" android:pivotX="53%">
                    <shape android:shape="line" android:tint="@color/white">
                        <stroke android:width="1dp"/>
                    </shape>
                </rotate>
            </item>
        </layer-list>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <scale android:scaleWidth="100%">
                <shape android:shape="rectangle">
                    <solid android:color="@color/bright_teal"/>
                    <corners
                        android:bottomRightRadius="10dp"
                        android:topRightRadius="10dp" />
                </shape>
            </scale>
        </clip>
    </item>
</layer-list>

嵌套层列表中的第二项是您的垂直线。您可以放置任意数量的垂直线,并通过更改android:pivotX的值来设置它们的位置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接