android中的方形边缘不确定进度条

时间:2013-05-11 16:28:11

标签: android android-layout

我正在尝试在xml中自定义不确定的进度条,但我找不到方法来对边缘进行平方。我从SDK资源中找到了默认的xml drawables,但它们只引用了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>

但它并没有改变形状。我发布了图片,但我还没有足够的声誉。 我错过了什么?谢谢你的帮助。

3 个答案:

答案 0 :(得分:11)

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

// 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是真实图像,我希望将其作为不确定进度条的背景重复。

这个解决方案对我有用。

答案 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

答案 2 :(得分:1)

我打算创建一个水平的不确定 ProgressBar 没有圆角,只是一遍又一遍地从左到右扫过。为此我创建了以下drawable:

<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时,我得到了正确的动画行为,但ProgressBar的大小变得很小,并且拒绝在整个页面中展开正如我在布局中设定的那样。

在绝望地完成这项工作之后,我决定创建一个自定义视图并自己制作动画。为此,我使用了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)的值,就可以设置动画扫描的速度。