【问题标题】:Android custom ProgressBar is distorted or doubledAndroid自定义ProgressBar变形或翻倍
【发布时间】:2014-06-04 15:47:10
【问题描述】:

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

<?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" />

但结果如下:

它在原始进度条旁边再次绘制可绘制对象。如果我将 wrap_content 用于进度条的宽度和高度,这就是我得到的:

****SOLUTION?:我无法想象为什么,但如果我以编程方式设置可绘制对象,它就可以工作......

【问题讨论】:

    标签: android android-drawable android-progressbar


    【解决方案1】:

    我遇到了完全相同的问题。问题是ProgressBar 会尝试复制它的drawable,它有更多的空间。如果没有足够的空间,则分别种植。

    您可以通过缩放项目来解决这个问题:

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

    每个项目都重复。

    【讨论】:

      【解决方案2】:

      我可以通过提供一个对我有用的自定义进度对话框类来帮助你。

      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.dismiss();

      如果您想要具有唯一进度的透明背景,只需执行以下操作:

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

      使用它..

      【讨论】:

      • 对不起;这与问题完全无关。
      • @OrhanC1 感谢您的好评。但是我在这里尝试通过其他方法来提供帮助。可能其他人可以使用它。
      • @RanjitPati - 帮助谁?问题不在于你的方法,而在于答案没有回答问题。
      【解决方案3】:

      您可以简单地制作自己的自定义进度条。在您的 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" />
      

      【讨论】:

      • 我需要一个逐帧自定义进度条。你只是给了我另一种可能具有相同效果的动画。这个问题与它无关。
      • 抱歉,在这种情况下您可能需要使用 ViewAnimator。
      • gaudicos.blogspot.hu/2013/10/customized-progress-bar.html查看最后一个进度条,我不需要使用ViewAnimator。
      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2017-12-06
        • 1970-01-01
        • 2012-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多