How to Customize a Progress Bar In Android

前端 未结 9 2301
鱼传尺愫
鱼传尺愫 2020-11-22 16:07

I am working on an app in which I want to show a ProgressBar, but I want to replace the default Android ProgressBar.

So how can I customize

相关标签:
9条回答
  • 2020-11-22 16:15

    in your xml

    <ProgressBar
            android:id="@+id/progressBar1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            style="@style/CustomProgressBar" 
            android:layout_margin="5dip" />
    

    And in res/values/styles.xml:

    <resources> 
            <style name="CustomProgressBar" parent="android:Widget.ProgressBar.Horizontal">
              <item name="android:indeterminateOnly">false</item>
              <item name="android:progressDrawable">@drawable/custom_progress_bar_horizontal</item>
              <item name="android:minHeight">10dip</item>
              <item name="android:maxHeight">20dip</item>
            </style>       
        <style name="AppTheme" parent="android:Theme.Light" />
    </resources>
    

    And custom_progress_bar_horizontal is a xml stored in drawable folder which defines your custom progress bar. For more detail see this blog.

    I hope this will help you.

    0 讨论(0)
  • 2020-11-22 16:15

    Simplest way to create customize a progress bar in Android:

    1. Initialize and show dialog:

      MyProgressDialog progressdialog = new MyProgressDialog(getActivity());
      progressdialog.show();
      
    2. Create method:

      public class MyProgressDialog extends AlertDialog {
            public MyProgressDialog(Context context) {
                super(context);
                getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            }
      
            @Override
            public void show() {
                super.show();
                setContentView(R.layout.dialog_progress);
            }
      }
      
    3. Create layout XML:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:clickable="true">
      
          <RelativeLayout
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_centerInParent="true">
      
            <ProgressBar
               android:id="@+id/progressbarr"
               android:layout_width="@dimen/eightfive"
               android:layout_height="@dimen/eightfive"
               android:layout_centerInParent="true"
              android:indeterminateDrawable="@drawable/progresscustombg" />
      
            <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_centerHorizontal="true"
               android:layout_below="@+id/progressbarr"
               android:layout_marginTop="@dimen/_3sdp"
               android:textColor="@color/white"
               android:text="Please wait"/>
          </RelativeLayout>
      </RelativeLayout>
      
    4. Create shape progresscustombg.xml and put res/drawable:

      <?xml version="1.0" encoding="utf-8"?>
      <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" >
      
          <shape
             android:innerRadiusRatio="3"
             android:shape="ring"
             android:thicknessRatio="20"
             android:useLevel="false" >
              <size
                  android:height="@dimen/eightfive"
                  android:width="@dimen/eightfive" />
      
              <gradient
                  android:centerY="0.50"
                  android:endColor="@color/color_green_icash"
                  android:startColor="#FFFFFF"
                  android:type="sweep"
                  android:useLevel="false" />
          </shape>
      
      </rotate>
      
    0 讨论(0)
  • 2020-11-22 16:19

    Creating Custom ProgressBar like hotstar.

    1. Add Progress bar on layout file and set the indeterminateDrawable with drawable file.

    activity_main.xml

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/player_progressbar"
        android:indeterminateDrawable="@drawable/custom_progress_bar"
        />
    
    1. Create new xml file in res\drawable

    custom_progress_bar.xml

    <?xml version="1.0" encoding="utf-8"?>
        <rotate  xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="1080" >
    
        <shape
            android:innerRadius="35dp"
            android:shape="ring"
            android:thickness="3dp"
            android:useLevel="false" >
           <size
               android:height="80dp"
               android:width="80dp" />
    
           <gradient
                android:centerColor="#80b7b4b2"
                android:centerY="0.5"
                android:endColor="#f4eef0"
                android:startColor="#00938c87"
                android:type="sweep"
                android:useLevel="false" />
        </shape>
    
    </rotate>
    
    0 讨论(0)
  • 2020-11-22 16:28

    For using custom drawable:

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

    (add under res/drawable progress.xml). my_drawable may be xml, png

    Then in your layout use

    <ProgressBar
            android:id="@+id/progressBar"
            android:indeterminateDrawable="@drawable/progress_circle"
    ...
    />
    
    0 讨论(0)
  • 2020-11-22 16:31

    In case of complex ProgressBar like this,

    enter image description here

    use ClipDrawable.

    NOTE : I've not used ProgressBar here in this example. I've achieved this using ClipDrawable by clipping image with Animation.

    A Drawable that clips another Drawable based on this Drawable's current level value. You can control how much the child Drawable gets clipped in width and height based on the level, as well as a gravity to control where it is placed in its overall container. Most often used to implement things like progress bars, by increasing the drawable's level with setLevel().

    NOTE : The drawable is clipped completely and not visible when the level is 0 and fully revealed when the level is 10,000.

    I've used this two images to make this CustomProgressBar.

    scall.png

    scall.png

    ballon_progress.png

    ballon_progress.png

    MainActivity.java

    public class MainActivity extends ActionBarActivity {
    
    private EditText etPercent;
    private ClipDrawable mImageDrawable;
    
    // a field in your class
    private int mLevel = 0;
    private int fromLevel = 0;
    private int toLevel = 0;
    
    public static final int MAX_LEVEL = 10000;
    public static final int LEVEL_DIFF = 100;
    public static final int DELAY = 30;
    
    private Handler mUpHandler = new Handler();
    private Runnable animateUpImage = new Runnable() {
    
        @Override
        public void run() {
            doTheUpAnimation(fromLevel, toLevel);
        }
    };
    
    private Handler mDownHandler = new Handler();
    private Runnable animateDownImage = new Runnable() {
    
        @Override
        public void run() {
            doTheDownAnimation(fromLevel, toLevel);
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        etPercent = (EditText) findViewById(R.id.etPercent);
    
        ImageView img = (ImageView) findViewById(R.id.imageView1);
        mImageDrawable = (ClipDrawable) img.getDrawable();
        mImageDrawable.setLevel(0);
    }
    
    private void doTheUpAnimation(int fromLevel, int toLevel) {
        mLevel += LEVEL_DIFF;
        mImageDrawable.setLevel(mLevel);
        if (mLevel <= toLevel) {
            mUpHandler.postDelayed(animateUpImage, DELAY);
        } else {
            mUpHandler.removeCallbacks(animateUpImage);
            MainActivity.this.fromLevel = toLevel;
        }
    }
    
    private void doTheDownAnimation(int fromLevel, int toLevel) {
        mLevel -= LEVEL_DIFF;
        mImageDrawable.setLevel(mLevel);
        if (mLevel >= toLevel) {
            mDownHandler.postDelayed(animateDownImage, DELAY);
        } else {
            mDownHandler.removeCallbacks(animateDownImage);
            MainActivity.this.fromLevel = toLevel;
        }
    }
    
    public void onClickOk(View v) {
        int temp_level = ((Integer.parseInt(etPercent.getText().toString())) * MAX_LEVEL) / 100;
    
        if (toLevel == temp_level || temp_level > MAX_LEVEL) {
            return;
        }
        toLevel = (temp_level <= MAX_LEVEL) ? temp_level : toLevel;
        if (toLevel > fromLevel) {
            // cancel previous process first
            mDownHandler.removeCallbacks(animateDownImage);
            MainActivity.this.fromLevel = toLevel;
    
            mUpHandler.post(animateUpImage);
        } else {
            // cancel previous process first
            mUpHandler.removeCallbacks(animateUpImage);
            MainActivity.this.fromLevel = toLevel;
    
            mDownHandler.post(animateDownImage);
        }
    }
    }
    

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <EditText
            android:id="@+id/etPercent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="number"
            android:maxLength="3" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ok"
            android:onClick="onClickOk" />
    
    </LinearLayout>
    
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
    
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/scall" />
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/clip_source" />
    
    </FrameLayout>
    

    clip_source.xml

    <?xml version="1.0" encoding="utf-8"?>
    <clip xmlns:android="http://schemas.android.com/apk/res/android"
        android:clipOrientation="vertical"
        android:drawable="@drawable/ballon_progress"
        android:gravity="bottom" />
    

    In case of complex HorizontalProgressBar just change cliporientation in clip_source.xml like this,

    android:clipOrientation="horizontal"
    

    You can download complete demo from here.

    0 讨论(0)
  • 2020-11-22 16:31

    Customizing the color of progressbar namely in case of spinner type needs an xml file and initiating codes in their respective java files.

    Create an xml file and name it as progressbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        tools:context=".Radio_Activity" >
    
        <LinearLayout
            android:id="@+id/progressbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <ProgressBar
                android:id="@+id/spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
            </ProgressBar>
        </LinearLayout>
    
    </LinearLayout>
    

    Use the following code to get the spinner in various expected color.Here we use the hexcode to display spinner in blue color.

    Progressbar spinner = (ProgressBar) progrees.findViewById(R.id.spinner);
    spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#80DAEB"),
                    android.graphics.PorterDuff.Mode.MULTIPLY);
    
    0 讨论(0)
提交回复
热议问题