Custom Drawable for ProgressBar/ProgressDialog

后端 未结 8 1010
野性不改
野性不改 2020-11-22 10:33

Reading the limited documentation that Google has provided, I get the feeling that it is possible to change the look (drawable) of a ProgressBar/ProgressDialog by simply cre

相关标签:
8条回答
  • 2020-11-22 10:38

    I was having some trouble using an Indeterminate Progress Dialog with the solution here, after some work and trial and error I got it to work.

    First, create the animation you want to use for the Progress Dialog. In my case I used 5 images.

    ../res/anim/progress_dialog_icon_drawable_animation.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>
    

    Where you want to show a ProgressDialog:

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

    This solution is really simple and worked for me, you could extend ProgressDialog and make it override the drawable internally, however, this was really too complicated for what I needed so I did not do it.

    0 讨论(0)
  • 2020-11-22 10:48

    Your style should look like this:

    <style parent="@android:style/Widget.ProgressBar" name="customProgressBar">
        <item name="android:indeterminateDrawable">@anim/mp3</item>
    </style>
    
    0 讨论(0)
  • 2020-11-22 10:50

    Custom progress with scale!

    <?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>
    
    0 讨论(0)
  • 2020-11-22 10:50

    I'm not sure but in this case you can still go with a complete customized AlertDialog by having a seperate layout file set in the alert dialog and set the animation for your imageview using part of your above code that should also do it!

    0 讨论(0)
  • 2020-11-22 10:56

    I used the following for creating a custom progress bar.

    File res/drawable/progress_bar_states.xml declares the colors of the different states:

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

    And the code inside your layout 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>
    

    Enjoy!

    0 讨论(0)
  • 2020-11-22 10:56

    Try setting:

    android:indeterminateDrawable="@drawable/progress" 
    

    It worked for me. Here is also the code for 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> 
    
    0 讨论(0)
提交回复
热议问题