1. Project requirements
Progress bars are often used in projects. They are very simple. Here is a simple summary and implementation.
2. Implement controls
ProgressBar
3. Implement the code
1. Horizontal progress bar
xml layout code:
<ProgressBar
android:id="@+id/rocketProgressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_centerHorizontal="true"
android:layout_marginHorizontal="60dp"
android:layout_marginBottom="80dp"
android:progressDrawable="@drawable/mmmmmm"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:progress="65" />
style attribute: determines what type of progress bar it is
progressDrawable property: Determine the background of the progress bar, the color of the progress bar, etc.
mmmmmm.xml: drawable file code
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#6DC9FF"/>
<corners android:radius="4dp" />
</shape>
</clip>
</item>
<item android:id="@android:id/background">
<shape>
<corners android:radius="4dp" />
<solid android:color="#4D6DC9FF" />
</shape>
</item>
</layer-list>
The properties inside are literal
Control code: For example, if I want to add a two-second progress bar to the opening page and then jump to the next interface, I can implement it like this:
lateinit var countDownTimer: CountDownTimer
countDownTimer = object : CountDownTimer(2000L, 200) {
override fun onTick(p0: Long) {
mBinding.rocketProgressBar.progress = ((2000 - p0) / 20).toInt() + 1
}
override fun onFinish() {
val intent = Intent(this@PhoneActivity, ComputerActivity::class.java)
startActivity(intent)
finish()
}
}
countDownTimer.start()
2. Ring-shaped progress bar
xml code:
<ProgressBar
android:id="@+id/circularProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:indeterminate="false"
android:max="100"
android:progressDrawable="@drawable/aaa"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:progress="80" />
aaa drawable file
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/progress">
<shape
android:shape="ring"
android:innerRadius="54dp"
android:thickness="2dp">
<solid android:color="#55CCFF" />
</shape>
</item>
</layer-list>
You can see that there is a big difference between here and above.