Android中两端圆角的进度条

14

我正在尝试在Android中创建自定义进度条。我已经使用以下XML文件进行了设置(progress_bar_horizontal.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">
   <shape>
    <corners android:radius="8dip" />
    <stroke android:width="2dip" android:color="#FFFF"/>
    <solid android:color="#FFFF"/>              
   </shape>
  </item>
  <item android:id="@android:id/secondaryProgress">
   <clip>
    <shape>
     <corners android:radius="8dip" />
     <stroke android:width="2dip" android:color="#FFFF"/>
     <solid android:color="#FF00"/> 
    </shape>
   </clip>
  </item>
  <item android:id="@android:id/progress">
   <clip>
    <shape>
     <corners android:radius="8dip" />
     <stroke android:width="2dip" android:color="#FFFF"/>
     <solid android:color="#FF00"/> 
    </shape>
   </clip>
  </item>
 </layer-list>

除了我想要进度条两侧都是圆角之外,一切都运行得很好。上述代码使进度条在左侧成为圆角,在右侧则只是简单的剪切(而非圆角)。这可能是由于clip标签引起的。你能帮我解决这个问题吗?我应该怎么改变才能让我的进度条两侧都是圆角呢?

完整的布局如下:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">
  <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:background="@drawable/gradient_progress"
  android:padding="10dip"
  >
   <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   >
    <TextView
     android:id="@+id/progress_header"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textColor="#FF000000"
     android:textAppearance="?android:attr/textAppearanceMedium"
     android:textStyle="bold"
     android:text="Uploading"               
    />
    <TextView
     android:id="@+id/progress_percent"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textColor="#FF000000"
     android:textAppearance="?android:attr/textAppearanceMedium"
     android:textStyle="bold"
     android:text="55%"         
     android:paddingLeft="10dip"
    />      
   </LinearLayout>          
   <ProgressBar
    android:id="@+id/progress_bar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:progressDrawable="@drawable/progress_bar_horizontal"
    android:maxHeight="12dip"    
    android:minHeight="12dip"    
    android:max="100" 
   />
  </LinearLayout>
 </LinearLayout>

这是我的努力的结果:链接文字

我希望红色条的右侧也有圆角。 非常感谢您的评论。


你能让我们看看你把这个东西放在哪里的布局吗? 我假设你在SDK中看到了progress_horizontal.xml。 - Alex Volovoy
感谢您的输入。是的,我已经看过框架示例,但不幸的是它们无法处理我的情况。我已经更新了问题的完整布局,并上传了显示当前条的图片。感谢您的考虑。 - youri
6个回答

32

以下是最新的答案:

Android源代码使用Patch 9文件来实现效果: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/res/res/drawable/progress_horizontal_holo_dark.xml/

因此,在您的布局xml中开始:

<ProgressBar
    android:id="@+id/custom_progress_bar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:indeterminateOnly="false"
    android:progressDrawable="@drawable/custom_progress_bar_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="13"
    android:progress="33"
    android:secondaryProgress="66" />

你可以将其中的一些内容移动到样式XML中,但这并不重要。我们真正关心的是 android:progressDrawable="@drawable/custom_progress_bar_horizontal" - 这将允许我们指定自定义进度条:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background"
          android:drawable="@android:drawable/custom_progress_bg" />
    <item android:id="@android:id/secondaryProgress">
        <scale android:scaleWidth="100%"
               android:drawable="@android:drawable/custom_progress_secondary" />
    </item>
    <item android:id="@android:id/progress">
        <scale android:scaleWidth="100%"
               android:drawable="@android:drawable/custom_progress_primary" />
    </item>
</layer-list>

或者您可以不使用Patch 9来设置背景-例如这里有一个简单的带有1dp边框的白色背景:

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="6.5dp" />

        <solid android:color="@android:color/white" />

        <stroke
            android:width="1dp"
            android:color="@android:color/darker_gray" />
    </shape>
</item>

Android Asset Studio 提供了一个非常棒的工具,帮助你生成点9图:http://android-ui-utils.googlecode.com/hg/asset-studio/dist/nine-patches.html

工具之前的示例主要是带有填充的 xdpi PNG 图像: Primary progress bar
工具之前的次要示例 xdpi PNG 带有填充: Secondary progress bar

最终输出如下: Custom progress bar


我正在遇到一个问题——在开头的部分,它看起来像是“挤压”了,你能给我提供一个示例吗? - Ando Masahashi
使用非9路径可绘制对象(形状)的比例技巧正是我一直在寻找的,非常棒的技巧 :) - guy_m

28

只需要像这样将标签<clip>更改为<scale>

<scale android:scaleWidth="100%">

我不确定为什么这没有得到更多的关注。它运行得非常好。谢谢! - Russell Stewart
1
那应该就是答案。 - Mikhail
它完美地运行了!这是最完美和最简单的方法。谢谢。 - SkyNetRush

4
您可以使用Material Components库提供的LinearProgressIndicatorapp:trackCornerRadius属性。类似于以下内容:
    <com.google.android.material.progressindicator.LinearProgressIndicator
        android:indeterminate="true"
        app:indicatorSize="xxdp"
        app:trackCornerRadius="xdp"/>

图片描述在此输入

注意:至少需要版本1.3.0-alpha04


圆形进度条的属性与我们是否相同? - itabdullah

3
据我所知,这是不可能的。因为在内部 ClipDrawable(即剪切标记)使用 canvas.clipRect() 剪切给定的形状/可绘制对象。顺便说一下,您可以从这里复制 ClipDrawable 的源代码,并基于进度级别剪切特定路径。

添加一个新项目到图层列表中,可以在剪切的进度条末端添加圆形结束。像这样:right:5dp; ... 当进度条达到100%或接近100%时,可能会出现问题,但无论如何,您是否考虑过这种方式?或者您已经想到了吗? - neteinstein
在图层列表中添加一个项目是没有用的,因为它的高度和宽度与所有其他图层相同。此外,您需要更改ProgressBar以处理新图层。 - Karan
无法打开此链接,请帮忙。 - parul

0
为了解决进度条在进度非常低的时候重叠的问题,我编写了一个扩展类来添加固定的起始偏移量到进度条上,同时保持其正常功能。
只需调用“setMaxWithPercentOffset()”或“setMaxWithOffset()”,而不是传递给“setMax()”所需的值作为起始偏移量即可。
如果有人已经解决了这个问题而不需要起始偏移,请告诉我!
/**
 * This progress bar can be used when the progress bar end graphics are styled in some way.
 * In this case, the progress bar must always have a small percentage of progress, otherwise
 * the styled ends of the progress overlap each other.
 *
 */
public class EndStyledProgressBar extends ProgressBar {
    private static final int DEFAULT_START_OFFSET_PERCENT = 5;

    private int mStartOffset;
    private int mRealMax;

    public EndStyledProgressBar(Context context) {
        super(context);
        commonConstructor();
    }

    public EndStyledProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        commonConstructor();
    }

    public EndStyledProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        commonConstructor();
    }

    private void commonConstructor() {
        mRealMax = super.getMax();
        mStartOffset = 0;

        setMaxWithPercentOffset(DEFAULT_START_OFFSET_PERCENT, mRealMax);

        super.setProgress(super.getProgress() + mStartOffset);
        super.setSecondaryProgress(super.getSecondaryProgress() + mStartOffset);
    }

    public void setProgress(int progress) {
        super.setProgress(progress + mStartOffset);
    }

    public void setSecondaryProgress(int secondaryProgress) {
        super.setSecondaryProgress(secondaryProgress + mStartOffset);
    }

    public int getProgress() {
        int realProgress = super.getProgress();
        return isIndeterminate() ? 0 : (realProgress - mStartOffset);
    }


    public  int getSecondaryProgress() {
        int realSecondaryProgress = super.getSecondaryProgress();
        return isIndeterminate() ? 0 : (realSecondaryProgress - mStartOffset);
    }

    public int getMax() {
        return mRealMax;
    }


    /**
     * Don't call this, instead call setMaxWithPercentOffset() or setStartOffsetInPercent()
     *
     * @param max
     */
    public void setMax(int max) {
        super.setMax(max);
    }

    /**
     * Sets a new max with a start offset (in percent) included.
     *
     * @param startOffsetInPercent start offset for the progress bar to avoid graphic errors.
     */
    public void setMaxWithPercentOffset(int startOffsetInPercent, int max) {
        mRealMax = max;
        int startOffset = (mRealMax * startOffsetInPercent) / 100;

        setMaxWithOffset(startOffset, max);
    }

    /**
     * Sets a new max with a start offset included.
     *
     * @param startOffset start offset for the progress bar to avoid graphic errors.
     */
    public void setMaxWithOffset(int startOffset, int max) {
        mRealMax = max;

        super.setMax(startOffset + max);

        setStartOffset(startOffset);

        super.setMax(startOffset + max);
    }


    /**
     * Sets a new start offset different from the default of 5%
     *
     * @param startOffset start offset for the progress bar to avoid graphic errors.
     */
    private void setStartOffset(int startOffset) {
        int newStartOffset = startOffset;

        // Ensure the start offset is not outside the range of the progress bar
        if (newStartOffset < 0) newStartOffset = 0;
        if (newStartOffset >= super.getMax()) newStartOffset = super.getMax();

        // Apply the start offset difference
        if (mStartOffset != newStartOffset) {
            int diff = newStartOffset - mStartOffset;
            super.setMax(super.getMax() + diff);
            super.setProgress(super.getProgress() + diff);
            super.setSecondaryProgress(super.getSecondaryProgress() + diff);
            mStartOffset = newStartOffset;
        }
    }
}

0

没有使用9-patch的最终圆角双边进度条是: android:progressDrawable="@drawable/progress_horizontal_green"

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/progress">
    <scale android:scaleWidth="100%">
        <shape>
            <corners android:radius="5dip"/>
            <solid android:color="#33FF33"/>
        </shape>
    </scale>
</item>


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