1

我的应用程序在 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);
}

实现此动画的简单方法是什么?逐帧动画会更容易做吗?

4

3 回答 3

1

使用 AsyncTask http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html它不是旋转的,因为它在 UI 线程中执行

于 2012-03-26T16:39:22.317 回答
1

您可能需要在 xml 或代码中为 android:indeterminate 属性设置 true 值

于 2012-03-26T16:40:13.690 回答
1

安卓:你做错了……

如果你在 UI 线程上做了一些需要很长时间的事情,你的应用就会冻结。由于您已锁定 UI 线程,因此您无法制作任何动画(您很幸运得到了两个滴答声),您无法让您的应用响应触摸或按键,并且您的用户将看到 ANR 屏幕(糟糕的用户体验)。永远不要在 UI 线程上执行任何长时间运行的任务,没有任何好的理由这样做。

我猜您想在 UI 线程上执行任务,因为您显示的内容取决于任务的结果?在这种情况下,在主线程上显示一个微调器,在后台运行任务(AsyncTask 正是为此而设计的),然后只在任务完成后更新你的 UI。没有可怕的用户体验,同样的最终结果。

于 2012-03-26T17:35:02.417 回答