我的应用程序有一个不那么频繁的操作,它在 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);
}
实现这个动画的简单方法是什么?逐帧动画会更容易做吗?
最佳答案
使用 AsyncTask http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html它不是自旋,因为它在 UI 线程中执行
关于android-animation - 长时间操作期间的微调动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9876096/