Android progress bar: ProgressBar and Seekbar

1. Introduction to ProgressBar progress bar

   ProgressBarIt is a progress bar control in Android, used to display the progress of ongoing tasks. It can display progress in a horizontal or circular format, and provides a variety of styles and attributes to meet different needs.

Related properties:

  1. android:progress: Set the current progress value of the progress bar.
  2. android:max: Set the maximum value of the progress bar.
  3. android:indeterminate: Set whether the progress bar is in indeterminate mode.
  4. android:progressDrawable: Set the progress bar Drawable of the progress bar, which can be a Drawable resource.
  5. android:indeterminateDrawable: A Drawable that sets the indeterminate mode of the progress bar. It can be a Drawable resource.
  6. android:secondaryProgress: Secondary progress bar, similar to video playback. One is the current playback progress and the other is the buffering progress. The former is set through the progress attribute!

Commonly used methods: 

  1. setProgress(int progress): Set the current progress value of the progress bar.
  2. setMax(int max): Set the maximum value of the progress bar.
  3. getProgress(): Get the progress value of the current progress bar.
  4. getMax(): Get the maximum value of the progress bar.
  5. setIndeterminate(boolean indeterminate): Set whether the progress bar is in indefinite mode, that is, it does not display specific progress but only displays an animation effect.
  6. isIndeterminate(): Determine whether the progress bar is in uncertain mode.
  7. setVisibility(int visibility): Set the visibility of the progress bar, which can be  VISIBLE, INVISIBLE or  GONE.
  8. setProgressDrawable(Drawable drawable): Set the progress bar Drawable of the progress bar, which can be a Drawable object or resource ID.
  9. setIndeterminateDrawable(Drawable drawable): Drawable that sets the indeterminate mode of the progress bar. It can be a Drawable object or resource ID.

2. SeekBar introduction:

  SeekBarIt is a slideable bar control in Android that allows users to select a value or adjust a certain setting by sliding. It is usually used for volume control, brightness adjustment, progress selection and other scenarios. SeekBar inherits from ProgressBar class.

Commonly used methods:

The following are SeekBarcommonly used methods:

  1. setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener listener): Set the listener of SeekBar to listen for sliding events and value change events.
  2. setMax(int max): Set the maximum value of SeekBar.
  3. getMax(): Get the maximum value of SeekBar.
  4. setProgress(int progress): Set the current progress value of SeekBar.
  5. getProgress(): Get the current progress value of SeekBar.
  6. setThumb(Drawable thumb): Set the image of the slider (Thumb), which can be a Drawable object or resource ID.
  7. setThumbOffset(int thumbOffset): Set the offset of the slider to adjust the position of the slider.
  8. getThumb(): Get the slider image used by the current SeekBar.
  9. getThumbOffset(): Get the offset of the current slider.

Related properties:

The following are SeekBarcommonly used properties:

  1. android:max: Set the maximum value of SeekBar.
  2. android:progress: Set the current progress value of SeekBar.
  3. android:thumb: Set the image of the slider (Thumb), which can be a Drawable resource.
  4. android:thumbOffset: Set the offset of the slider.
  5. android:progressDrawable: Set the progress bar of SeekBar to Drawable, which can be a Drawable resource. 

3. Examples 

 operation result:

MainActivity : 



public class MainActivity extends AppCompatActivity {
private Button add,minus;
ProgressBar mProgressBar;
SeekBar mSeekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add = findViewById(R.id.add);
        mProgressBar = findViewById(R.id.bar);
        minus = findViewById(R.id.minus);
        mSeekBar = findViewById(R.id.seekbar);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int curProgress = mProgressBar.getProgress();
                int curSeekProgress = mSeekBar.getProgress();
                mSeekBar.setProgress(curSeekProgress+10);
                mProgressBar.setProgress(curProgress+10);
            }
        });
        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int curProgress = mProgressBar.getProgress();
                mProgressBar.setProgress(curProgress-10);
                int curSeekProgress = mSeekBar.getProgress();
                mSeekBar.setProgress(curSeekProgress-10);
            }
        });
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Toast.makeText(MainActivity.this, "进度条进度改变!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(MainActivity.this, "进度条被手指触摸!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(MainActivity.this, "手指离开进度条!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 activity_main:

<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:orientation="vertical"
    tools:context=".MainActivity">

   <ProgressBar
       android:id="@+id/bar"
       android:gravity="center"
       style="@android:style/Widget.ProgressBar.Horizontal"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:max="100"
       android:min="0"
       android:progress="18" />
   <SeekBar
       android:id="@+id/seekbar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:max="100"
      android:min="0"/>
   <Button
       android:id="@+id/add"
       android:layout_width="wrap_content"
       android:layout_height="50dp"
       android:text="增加进度"
       android:layout_gravity="center"
      />
   <Button
       android:id="@+id/minus"
       android:layout_width="wrap_content"
       android:layout_height="50dp"
       android:text="减少进度"
       android:layout_gravity="center"
       />

</LinearLayout>

 

 

Guess you like

Origin blog.csdn.net/A125679880/article/details/131736127