安卓系统中的方边不确定进度条

6

我正在尝试在xml中自定义一个不确定进度条,但我找不到方法将边角变成正方形。我已经找到了SDK资源中的默认xml可绘制文件,但它们只是引用了没有形状说明的PNG文件。

以下是来自SDK资源的默认xml:

<?xml version="1.0" encoding="utf-8"?>
<animation-list
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">
    <item android:drawable="@drawable/progressbar_indeterminate1" android:duration="200" />
    <item android:drawable="@drawable/progressbar_indeterminate2" android:duration="200" />
    <item android:drawable="@drawable/progressbar_indeterminate3" android:duration="200" />
</animation-list>

progressbar_indeterminate1、2 和 3 只是方形 PNG,但它总是以圆角边缘显示进度条。

我尝试创建一个形状,并使用 PNG 作为背景,代码如下:

<animation-list
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">

   <item
        android:drawable="@drawable/progressbar_indeterminate1"
        android:duration="200">
        <shape>
            <corners android:radius="0dp" />
        </shape>
    </item>

</animation-list>

但它并不改变形状。我想发图片,但我还没有足够的声望。

我错过了什么?感谢您的任何帮助。


看一下那些PNG文件。我怀疑它们本身包含了相关的形状。 - Aleks G
不行,我已经尝试过了。它们只是方形的图像。 - Ben De La Haye
这些图片自然是正方形的 - 但是图片中有什么? - Aleks G
进度条的背景只是一小块斜灰线,没有圆角或其他任何东西。我可以把它们发布出来,但由于声望不够,我无法这样做。 - Ben De La Haye
3个回答

12

我遇到了同样的问题,似乎使用XML设置的不定框架具有圆角。如果您需要方形(或可能是其他)边缘,则需要使用代码设置不确定绘图:

我刚遇到了同样的问题,似乎使用XML设置的不定框架具有圆角。如果您需要方形(或者可能是任何其他)边缘,那么您需要使用代码设置不定框架:

// line below is needed to have sharp corners in the indeterminate drawable,
// ProgressBar when parsing indeterminate drawable from XML sets rounded corners.
progressBar.setIndeterminateDrawable(context.getResources().getDrawable(R.drawable.progressbar_indeterminate));

然后是我使用的progressbar_indeterminate示例:

<?xml version="1.0" encoding="UTF-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
 <item android:drawable="@drawable/progress_2b" android:duration="100" />
 <item android:drawable="@drawable/progress_1b" android:duration="100" />
</animation-list>

我需要重复使用图片,所以我创建了progress_1b(和2b),如下所示:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/progress_1"
    android:tileMode="repeat" >
</bitmap>`

drawable/progress_1 是我想要作为不确定进度条背景重复显示的真实图片。

这个解决方案对我有效。


6
这对我很有帮助。没有PNG,只有颜色。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <corners android:radius="0dip" />
        <stroke android:width="2px" android:color="#80c4c4c4"/>
        <solid android:color="#08000000"/>
    </shape>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="0dip" />
            <solid android:color="#11416a"/>
        </shape>
    </clip>
</item>
</layer-list>

Progress Bar


是的,我已经像那样自定义了0-100%的进度部分。问题在于不确定模式。当它变为不确定时,我最终得到一个方形进度条,而它应该是圆形的。 - Ben De La Haye

1
我想创建一个水平的不确定进度条,没有圆角,只是简单地从左到右来回扫过。为此,我创建了以下可绘制对象:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape android:shape="rectangle">
            <solid android:color="@color/white"/>
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape android:shape="rectangle">
                <solid android:color="@color/app_orange"/>
            </shape>
        </clip>
    </item>

</layer-list>

这对于普通的ProgressBar很好用,但当我将其设置为ProgressBar布局文件中的indeterminateDrawable时,它的大小变得非常小,而且拒绝跨越整个页面,就像我在布局中设置的那样。在绝望地尝试使其工作之后,我决定创建一个自定义视图并自己进行动画处理。为此,我使用了API 11中引入的ObjectAnimator。为了在API 10(及更低版本)中使用它,我包含了NineYearOldAndroid项目的jar文件。效果非常好。最后一步是创建我的自定义视图:
public class ProgressLoad extends ProgressBar
{
    public ProgressLoad(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        ObjectAnimator animator = ObjectAnimator.ofInt(this, "progress", 0, 101);         // Need the 101 for the progress bar to show to the end.
        animator.setDuration(2000);
        animator.setRepeatCount(ObjectAnimator.INFINITE);
        animator.setRepeatMode(ObjectAnimator.RESTART);

        animator.start();
    }
}

然后我只需将这个自定义视图包含在我的布局中:
<com.example.views.ProgressLoad
            android:id="@+id/pb_load"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:layout_gravity="top"
            android:progressDrawable="@drawable/content_progress_bar_determinate"/>

瞧,我有一个水平的长方形进度条(没有圆角),进度条会重复地扫过。当我完成它时,我只需将自定义视图从其父级中删除即可。

这种技术的优点是,通过简单地改变传递给animator.setDuration(int value)的值,可以设置动画扫过的速度。


提醒一下,这并不能产生真正的不定进度条。它会将进度加载到100,然后重新开始,而不定进度条具有设置的进度(比如25),该进度在进度条的长度上扫过,因此当它向结束移动时,开头是空的。 - CodyEngel

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