1. Introduction to ProgressBar progress bar
ProgressBar
It 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:
android:progress
: Set the current progress value of the progress bar.android:max
: Set the maximum value of the progress bar.android:indeterminate
: Set whether the progress bar is in indeterminate mode.android:progressDrawable
: Set the progress bar Drawable of the progress bar, which can be a Drawable resource.android:indeterminateDrawable
: A Drawable that sets the indeterminate mode of the progress bar. It can be a Drawable resource.- 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:
setProgress(int progress)
: Set the current progress value of the progress bar.setMax(int max)
: Set the maximum value of the progress bar.getProgress()
: Get the progress value of the current progress bar.getMax()
: Get the maximum value of the progress bar.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.isIndeterminate()
: Determine whether the progress bar is in uncertain mode.setVisibility(int visibility)
: Set the visibility of the progress bar, which can beVISIBLE
,INVISIBLE
orGONE
.setProgressDrawable(Drawable drawable)
: Set the progress bar Drawable of the progress bar, which can be a Drawable object or resource ID.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:
SeekBar
It 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 SeekBar
commonly used methods:
setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener listener)
: Set the listener of SeekBar to listen for sliding events and value change events.setMax(int max)
: Set the maximum value of SeekBar.getMax()
: Get the maximum value of SeekBar.setProgress(int progress)
: Set the current progress value of SeekBar.getProgress()
: Get the current progress value of SeekBar.setThumb(Drawable thumb)
: Set the image of the slider (Thumb), which can be a Drawable object or resource ID.setThumbOffset(int thumbOffset)
: Set the offset of the slider to adjust the position of the slider.getThumb()
: Get the slider image used by the current SeekBar.getThumbOffset()
: Get the offset of the current slider.
Related properties:
The following are SeekBar
commonly used properties:
android:max
: Set the maximum value of SeekBar.android:progress
: Set the current progress value of SeekBar.android:thumb
: Set the image of the slider (Thumb), which can be a Drawable resource.android:thumbOffset
: Set the offset of the slider.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>