代码之家  ›  专栏  ›  技术社区  ›  Sam

progressbar/progressdialog的自定义绘图

  •  119
  • Sam  · 技术社区  · 16 年前

    阅读google提供的有限文档后,我感觉只要创建一个新样式并将其分配给progressbar的style属性,就可以更改progressbar/progressdialog的外观(可绘制)。但我不能让它正常工作。以下是我迄今为止所做的:

    我创建了一个这样的形状(mp2.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="ring"
      android:innerRadiusRatio="4"
      android:thicknessRatio="4"
      android:useLevel="false">
     <size android:width="50dip" android:height="50dip" />
     <gradient android:type="sweep" android:useLevel="false" android:startColor="#300000ff" android:centerColor="#500000ff" android:endColor="#ff0000ff" />
    </shape>
    

    然后创建了如下动画(mp3.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" android:toDegrees="30" android:repeatCount="1" />
     </item>
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="30" android:toDegrees="60" android:repeatCount="1" />
     </item>
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="60" android:toDegrees="90" android:repeatCount="1" />
     </item>
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="90" android:toDegrees="120" android:repeatCount="1" />
     </item>
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="120" android:toDegrees="150" android:repeatCount="1" />
     </item>
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="150" android:toDegrees="180" android:repeatCount="1" />
     </item>
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="180" android:toDegrees="210" android:repeatCount="1" />
     </item>
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="210" android:toDegrees="240" android:repeatCount="1" />
     </item>
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="240" android:toDegrees="270" android:repeatCount="1" />
     </item>
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="270" android:toDegrees="300" android:repeatCount="1" />
     </item>
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="300" android:toDegrees="330" android:repeatCount="1" />
     </item>
     <item android:duration="70">
      <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/mp2" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="330" android:toDegrees="360" android:repeatCount="1" />
     </item>
    </animation-list>
    

    然后创建如下样式(attrs.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
     <style parent="@android:style/Widget.ProgressBar" name="customProgressBar">
      <item name="android:progressDrawable">@anim/mp3</item>
     </style>
    </resources>
    

    在main.xml中,我设置了如下样式:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:orientation="vertical"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" android:drawingCacheQuality="high">
     <ProgressBar android:id="@+id/ProgressBar01"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" style="@style/customProgressBar"/>
    </LinearLayout>
    

    但它仍然显示出和以前一样的可拉伸性。我做错什么了?

    8 回复  |  直到 15 年前
        1
  •  138
  •   buczek George Weber    8 年前

    我用下面的方法创建了一个自定义进度条。

    文件 res/drawable/progress_bar_states.xml 声明不同状态的颜色:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:id="@android:id/background">
            <shape>
                <gradient
                        android:startColor="#000001"
                        android:centerColor="#0b131e"
                        android:centerY="0.75"
                        android:endColor="#0d1522"
                        android:angle="270"
                />
            </shape>
        </item>
    
        <item android:id="@android:id/secondaryProgress">
            <clip>
                <shape>
                    <gradient
                            android:startColor="#234"
                            android:centerColor="#234"
                            android:centerY="0.75"
                            android:endColor="#a24"
                            android:angle="270"
                    />
                </shape>
            </clip>
        </item>
    
        <item android:id="@android:id/progress">
            <clip>
                <shape>
                    <gradient
                        android:startColor="#144281"
                        android:centerColor="#0b1f3c"
                        android:centerY="0.75"
                        android:endColor="#06101d"
                        android:angle="270"
                    />
                </shape>
            </clip>
        </item>
    
    </layer-list>
    

    以及布局XML中的代码:

    <ProgressBar android:id="@+id/progressBar"
        android:progressDrawable="@drawable/progress_bar_states"
        android:layout_width="fill_parent" android:layout_height="8dip" 
        style="?android:attr/progressBarStyleHorizontal" 
        android:indeterminateOnly="false" 
        android:max="100">
    </ProgressBar>
    

    享受吧!

        2
  •  54
  •   Willi Mentzel user670265    9 年前

    我在使用带有解决方案的不确定进度对话框时遇到了一些问题,经过一些工作和反复尝试后,我使它开始工作。

    首先,创建要用于进度对话框的动画。在我的例子中,我使用了5个图像。

    ../res/anim/progress_dialog_icon_drawable_动画.xml:

    <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/icon_progress_dialog_drawable_1" android:duration="150" />
        <item android:drawable="@drawable/icon_progress_dialog_drawable_2" android:duration="150" />
        <item android:drawable="@drawable/icon_progress_dialog_drawable_3" android:duration="150" />
        <item android:drawable="@drawable/icon_progress_dialog_drawable_4" android:duration="150" />
        <item android:drawable="@drawable/icon_progress_dialog_drawable_5" android:duration="150" />
    </animation-list>
    

    要显示进度对话框的位置:

    dialog = new ProgressDialog(Context.this);
    dialog.setIndeterminate(true);
    dialog.setIndeterminateDrawable(getResources().getDrawable(R.anim.progress_dialog_icon_drawable_animation));
    dialog.setMessage("Some Text");
    dialog.show();
    

    这个解决方案非常简单,对我来说很有效,您可以扩展progressdialog并使其在内部重写drawable,但是,这对于我需要的东西来说太复杂了,所以我没有这么做。

        3
  •  34
  •   mudit    14 年前

    尝试设置:

    android:indeterminateDrawable="@drawable/progress" 
    

    它对我起作用。下面是progress.xml的代码:

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
        android:toDegrees="360">
    
        <shape android:shape="ring" android:innerRadiusRatio="3"
            android:thicknessRatio="8" android:useLevel="false">
    
            <size android:width="48dip" android:height="48dip" />
    
            <gradient android:type="sweep" android:useLevel="false"
                android:startColor="#4c737373" android:centerColor="#4c737373"
                android:centerY="0.50" android:endColor="#ffffd300" />
    
        </shape>
    
    </rotate> 
    
        4
  •  10
  •   Victor    15 年前

    你的风格应该是这样的:

    <style parent="@android:style/Widget.ProgressBar" name="customProgressBar">
        <item name="android:indeterminateDrawable">@anim/mp3</item>
    </style>
    
        5
  •  6
  •   Dmitry    12 年前

    定制进度与规模!

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:duration="150">
            <scale
                android:drawable="@drawable/face_no_smile_eyes_off"
                android:scaleGravity="center" />
        </item>
        <item android:duration="150">
            <scale
                android:drawable="@drawable/face_no_smile_eyes_on"
                android:scaleGravity="center" />
        </item>
        <item android:duration="150">
            <scale
                android:drawable="@drawable/face_smile_eyes_off"
                android:scaleGravity="center" />
        </item>
        <item android:duration="150">
            <scale
                android:drawable="@drawable/face_smile_eyes_on"
                android:scaleGravity="center" />
        </item>
    
    </animation-list>
    
        6
  •  5
  •   Renjith K N    13 年前

    我做你的代码。我可以运行,但我需要修改两个地方:

    1. name="android:indeterminateDrawable" 而不是 android:progressDrawable

    2. 修改名称attrs.xml--->styles.xml

        7
  •  0
  •   Willi Mentzel user670265    9 年前

    我不确定,但在这种情况下,您仍然可以通过在警报对话框中设置一个单独的布局文件来使用一个完整的自定义警报对话框,并使用上述代码的一部分为您的imageview设置动画,也应该这样做!

        8
  •  0
  •   VV W    8 年前
    public class CustomProgressBar {
        private RelativeLayout rl;
        private ProgressBar mProgressBar;
        private Context mContext;
        private String color__ = "#FF4081";
        private ViewGroup layout;
        public CustomProgressBar (Context context, boolean isMiddle, ViewGroup layout) {
            initProgressBar(context, isMiddle, layout);
        }
    
        public CustomProgressBar (Context context, boolean isMiddle) {
            try {
                layout = (ViewGroup) ((Activity) context).findViewById(android.R.id.content).getRootView();
            } catch (Exception e) {
                e.printStackTrace();
            }
            initProgressBar(context, isMiddle, layout);
        }
    
        void initProgressBar(Context context, boolean isMiddle, ViewGroup layout) {
            mContext = context;
            if (layout != null) {
                int padding;
                if (isMiddle) {
                    mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleSmall);
                    // mProgressBar.setBackgroundResource(R.drawable.pb_custom_progress);//Color.parseColor("#55000000")
                    padding = context.getResources().getDimensionPixelOffset(R.dimen.padding);
                } else {
                    padding = context.getResources().getDimensionPixelOffset(R.dimen.padding);
                    mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleSmall);
                }
                mProgressBar.setPadding(padding, padding, padding, padding);
                mProgressBar.setBackgroundResource(R.drawable.pg_back);
                mProgressBar.setIndeterminate(true);
                    try {
                        color__ = AppData.getTopColor(context);//UservaluesModel.getAppSettings().getSelectedColor();
                    } catch (Exception e) {
                        color__ = "#FF4081";
                    }
                    int color = Color.parseColor(color__);
    //                color=getContrastColor(color);
    //                color__ = color__.replaceAll("#", "");//R.color.colorAccent
                    mProgressBar.getIndeterminateDrawable().setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_ATOP);
                } 
                }
    
                RelativeLayout.LayoutParams params = new
                        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                rl = new RelativeLayout(context);
                if (!isMiddle) {
                    int valueInPixels = (int) context.getResources().getDimension(R.dimen.padding);
                    lp.setMargins(0, 0, 0, (int) (valueInPixels / 1.5));//(int) Utils.convertDpToPixel(valueInPixels, context));
                    rl.setClickable(false);
                    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                } else {
                    rl.setGravity(Gravity.CENTER);
                    rl.setClickable(true);
                }
                lp.addRule(RelativeLayout.CENTER_IN_PARENT);
                mProgressBar.setScaleY(1.55f);
                mProgressBar.setScaleX(1.55f);
                mProgressBar.setLayoutParams(lp);
    
                rl.addView(mProgressBar);
                layout.addView(rl, params);
            }
        }
    
        public void show() {
            if (mProgressBar != null)
                mProgressBar.setVisibility(View.VISIBLE);
        }
    
        public void hide() {
            if (mProgressBar != null) {
                rl.setClickable(false);
                mProgressBar.setVisibility(View.INVISIBLE);
            }
        }
    }
    

    然后打电话

    customProgressBar = new CustomProgressBar (Activity, true);
    customProgressBar .show();