我正在尝试实现一个循环下载进度条,它可以显示不同的可绘制
- 当进度为0时(下载未启动(
- 当进度>0&<100(正在下载(
- 进度为100时(下载完成(
当进度>0&<100带有以下代码
circle.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:thicknessRatio="16"
android:useLevel="false">
<solid android:color="#DDD" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="270"
android:toDegrees="270">
<shape
android:shape="ring"
android:thicknessRatio="16"
android:useLevel="true">
<gradient
android:endColor="@color/colorPrimary"
android:startColor="@color/colorAccent"
android:type="sweep" />
</shape>
</rotate>
</item>
</layer-list>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progress_bar"
style="@style/CircularDeterminateProgressBar"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:progress="60" />
<TextView
android:id="@+id/text_view_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintBottom_toBottomOf="@+id/progress_bar"
app:layout_constraintEnd_toEndOf="@+id/progress_bar"
app:layout_constraintStart_toStartOf="@+id/progress_bar"
app:layout_constraintTop_toTopOf="@+id/progress_bar"
tools:text="60%" />
<Button
android:id="@+id/button_decr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="- 10%"
app:layout_constraintStart_toStartOf="@+id/progress_bar"
app:layout_constraintTop_toBottomOf="@+id/progress_bar" />
<Button
android:id="@+id/button_incr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+ 10%"
app:layout_constraintEnd_toEndOf="@+id/progress_bar"
app:layout_constraintTop_toBottomOf="@+id/progress_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的活动如下
class MainActivity : AppCompatActivity() {
private var progr = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
updateProgressBar()
button_incr.setOnClickListener {
if (progr <= 90) {
progr += 10
updateProgressBar()
}
}
button_decr.setOnClickListener {
if (progr >= 10) {
progr -= 10
updateProgressBar()
}
}
}
private fun updateProgressBar() {
progress_bar.progress = progr
text_view_progress.text = "$progr%"
}
}
我不知道当下载进度为0(未启动(时如何设置不同的可绘制内容(下载图标(,当下载进度是100时如何设置其他不同的可绘图内容(下载完成图标(。
有什么建议或解决方案真的有用吗?
解决方案1:使用LevelListDrawable。您将能够定义用于不同级别的不同绘图。
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0" android:drawable="@drawable/ic_wifi_signal_1" />
<item android:maxLevel="1" android:drawable="@drawable/ic_wifi_signal_2" />
<item android:maxLevel="2" android:drawable="@drawable/ic_wifi_signal_3" />
<item android:maxLevel="3" android:drawable="@drawable/ic_wifi_signal_4" />
</level-list>
文件:
https://developer.android.com/reference/android/graphics/drawable/LevelListDrawable
解决方案2:无论何时更新进度条级别,都可以使用ProgressBar.setIndeterminateDrawable((动态更新可绘制内容。
文件:
https://developer.android.com/reference/android/widget/ProgressBar#setIndeterminateDrawable(android.graphics.drawable.drawable(