我的应用程序在 UI 线程上执行的操作不那么频繁,并且需要很长时间(最多 3 秒)。我想在那段时间显示动画“等待”指示。例如,旋转的微调器。无需显示实际进度,只需一个定速动画即可。
我创建了一个在长时间操作期间弹出的自定义对话框,它有这个布局
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
问题是它不旋转。即使 UI 线程很忙,如何让它旋转?
我试图创建一个事件链来增加它,但我只得到两个事件,可能是因为 UI 线程很忙。
// In the custom dialog class. mProgressBar is the ProgressBar in the layout.
// Called externally once when the dialog is shown
public void tick() {
mProgressBar.incrementProgressBy(10);
mProgressBar.postDelayed(new Runnable() {
@Override
public void run() {
// Not a recursion since it's done in a future event.
tick();
}
}, 100);
}
实现此动画的简单方法是什么?逐帧动画会更容易做吗?