在android上样式不确定的水平进度条

在android上样式不确定的水平进度条

本文介绍了在android上样式不确定的水平进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定进度条的样式很容易,有很多教程可以实现这一点.这是我正在使用的:

Styling determinate progress bar is easy, there are many tutorials to achieve that. This is on I'm using:

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="8dp"
    android:background="@drawable/progress_background"
    android:progressDrawable="@drawable/progress"
    android:id="@+id/progressBar" />

Drawable progress_background.xml:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/colorAccent" />
                <corners android:radius="4dp" />
            </shape>
        </clip>
    </item>
</layer-list>

看起来像这样:

但是当进展尚不可用时,我想使用不确定的.所以我尝试了:

But when progress is not available yet, I'd like to use indeterminate one. So I tried:

<ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="8dp"
        android:background="@drawable/progress_background"
        android:progressDrawable="@drawable/progress"
        android:indeterminateTint="@color/colorAccent"
        android:indeterminateTintMode="src_in"
        android:indeterminate="true"
        android:id="@+id/progressBar" />

但不确定的进度不会拉伸到条形高度:

But indeterminate progress is not stretched to bar height:

我也尝试使用可绘制文件对其进行样式设置,但它看起来像是损坏的确定进度条(再次从 0 填充到 100).

I tried to style it using drawable file as well but it looked like broken determinate progress bar (filling from 0 to 100 all over again).

所需的不确定进度条应该看起来像普通进度条,但具有 8dp 的高度和圆角.

Desired indeterminate progress bar should look like regular one but with 8dp height and rounded corners.

推荐答案

默认的不确定进度条动画使用非 9-patch png.所以它不能在你的 8dp 高度进度条上拉伸.您应该添加带有自定义动画的 android:indeterminateDrawable:

Default indeterminate progress bar animation use a non 9-patch pngs. So it can't be stretched over your 8dp height progress bar. You should add android:indeterminateDrawable with custom animation:

<animation-list
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">
    <item android:drawable="@drawable/progressbar_indeterminate1" android:duration="50" />
    <item android:drawable="@drawable/progressbar_indeterminate2" android:duration="50" />
    <item android:drawable="@drawable/progressbar_indeterminate3" android:duration="50" />
    ...
    <item android:drawable="@drawable/progressbar_indeterminateX" android:duration="50" />
</animation-list>

然后制作 drawables 使其像这样动画(可以是 xml 或图像或 9-patch):

Then make drawables to animate it like this (it can be xml or image or 9-patch):

动画帧 1

动画帧 2

Android 的任何版本 (api21+) 都不会使用 AnimatedVectorDrawable 来处理不确定的 ProgressBar.

Never versions of android (api21+) use AnimatedVectorDrawable for indeterminate ProgressBar.

这篇关于在android上样式不确定的水平进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:23