【问题标题】:How to draw a vertical line in "android Horizontal Progress bar"如何在“android水平进度条”中画一条垂直线
【发布时间】:2012-11-17 10:20:26
【问题描述】:

我需要制作一个自定义水平进度条,如下图所示。

我已经使用图层列表设计了如下图所示的进度条 可绘制:

我想在进度大于 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);
}

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

【问题讨论】:

    标签: android android-xml android-progressbar


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      在我看来,当它大于 25 时,你必须渲染那条线。 试试这个,让我知道

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

      【讨论】:

        【解决方案3】:

        问题很老了,但仍然出现在谷歌上。我不确定您是否希望垂直线高于或低于进度状态。阿里的回答使这条线出现在上面,我希望这条线出现在下面所以这是我的图层列表版本:

        <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 值来设置它们的位置。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-21
          • 1970-01-01
          • 2017-06-17
          相关资源
          最近更新 更多