Android自定义进度条扭曲或加倍

Android自定义进度条扭曲或加倍,android,android-drawable,android-progressbar,Android,Android Drawable,Android Progressbar,我只是在创建自定义进度条时遇到了一个我不理解的问题。我使用常用方法创建动画列表,然后将其应用于progressbar: <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable

我只是在创建自定义进度条时遇到了一个我不理解的问题。我使用常用方法创建动画列表,然后将其应用于progressbar:

<?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/dw_pb_1"
        android:duration="100"/>
    <item
        android:drawable="@drawable/dw_pb_2"
        android:duration="100"/>
    <item
        android:drawable="@drawable/dw_pb_3"
        android:duration="100"/>

</animation-list>
 <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="150"
            android:layout_height="150"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:indeterminate="true"
            android:indeterminateDrawable="@drawable/dw_loader" />

我将其应用于progressbar:

<?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/dw_pb_1"
        android:duration="100"/>
    <item
        android:drawable="@drawable/dw_pb_2"
        android:duration="100"/>
    <item
        android:drawable="@drawable/dw_pb_3"
        android:duration="100"/>

</animation-list>
 <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="150"
            android:layout_height="150"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:indeterminate="true"
            android:indeterminateDrawable="@drawable/dw_loader" />

但结果如下:

它会在原始progressbar旁边再次绘制可绘制图形。如果我对progressbar的宽度和高度使用wrap_内容,我得到的结果是:


****解决方案?:我无法想象为什么,但如果我以编程方式设置drawable,它会工作…

我可以通过提供一个自定义progressdialog类来帮助您,该类对我很有用

MyProgressDialog.class

public class MyProgressDialog extends Dialog {

public static MyProgressDialog show(Context context, CharSequence title,
        CharSequence message) {
    return show(context, title, message, false);
}

public static MyProgressDialog show(Context context, CharSequence title,
        CharSequence message, boolean indeterminate) {
    return show(context, title, message, indeterminate, false, null);
}

public static MyProgressDialog show(Context context, CharSequence title,
        CharSequence message, boolean indeterminate, boolean cancelable) {
    return show(context, title, message, indeterminate, cancelable, null);
}

public static MyProgressDialog show(Context context, CharSequence title,
        CharSequence message, boolean indeterminate,
        boolean cancelable, OnCancelListener cancelListener) {
    MyProgressDialog dialog = new MyProgressDialog(context);
    dialog.setTitle(title);
    dialog.setCancelable(cancelable);
    dialog.setOnCancelListener(cancelListener);
    /* The next line will add the ProgressBar to the dialog. */
    dialog.addContentView(new ProgressBar(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    dialog.show();

    return dialog;
}

public MyProgressDialog(Context context) {
    super(context, R.style.NewDialog);
}
然后像这样使用它:

MyProgressDialog mProgress = MyProgressDialog.show(context, title, message);
按相同的方式解散:
mProgress.disclose()

如果您想要透明的背景,只需执行以下操作:

 MyProgressDialog mProgress = MyProgressDialog.show(context, null, null);

使用它。

您只需制作自己的自定义进度条即可。在res/drawable中,创建一个XML文件,将其命名为loading_indertiminate.XML,例如:

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/loading_spinner"
    android:pivotX="50%"
    android:pivotY="50%" />

并且,在你的布局中,只需为你的进度条做这个

<ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:indeterminateDrawable="@drawable/loading_indertiminate" />

我也有同样的问题。问题是,
ProgressBar
将尝试复制其可绘制属性,因为它有更多的空间。如果没有足够的空间,分别种植作物

可以通过缩放项目来解决此问题:

<item android:duration="100">
        <scale
            android:drawable="@drawable/@drawable/dw_pb_1"
            android:scaleGravity="center" />
</item>


每个项目都会重复。

自Android版本6.0(棉花糖API级别23)以来,此问题不再出现。
对于以前版本的支持,@Cranker提供的解决方案仍然有效。

我需要一个逐帧自定义进度条。你刚才给了我另一种可能有同样效果的动画。问题与此无关。很抱歉,在这种情况下,您可能需要使用ViewAnimator。检查最后一个进度条,我不需要使用ViewAnimator;这与问题完全无关。@OrhanC1谢谢你的评论。但是我试着用另一种方法来帮助你。可能有其他人可以使用它。@RanjitPati-帮助谁?问题不在于你的方法,而是答案不能回答问题。