2

我正在尝试为进度条设置动画,以便线性填充需要 1 分钟。

这是我的代码:

animator = ObjectAnimator.ofInt(progress_bar, "progress", 0, 100);
animator.setDuration(1000 * 60);
animator.setInterpolator(new LinearInterpolator());
animator.start();

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="match_parent"
    android:layout_height="9sp"
    android:layout_centerHorizontal="true"
    android:max="100"
    android:min="0"
    android:progress="0"
    android:progressDrawable="@drawable/custom_progress_bar"
    android:visibility="visible" />

进度条填充太快,大约 20 秒,而不是 1 分钟

4

2 回答 2

2

在 Developer Options 下,有选项 Animator duration scale。这可能是导致您的问题的原因。

希望能帮助到你。

于 2019-07-12T16:12:20.443 回答
1

该问题是由 Android 的开发人员选项下的动画持续时间比例设置为 0.5 引起的。

ValueAnimator 类中有一个setDurationScale()方法。它是隐藏的,但您可以使用反射调用它,并使用它将持续时间比例设置为 1:

try {
    ValueAnimator.class.getMethod("setDurationScale", float.class).invoke(null, 1f);
} catch (Throwable t) {
    Log.e(this.getClass().getName(), t.getMessage());
}

这将使 ObjectAnimator 独立于全局动画持续时间比例。

如果您需要保存动画持续时间比例的当前选项值:

// Get duration scale from the global settings.
float durationScale = Settings.Global.getFloat(context.getContentResolver(),
                        Settings.Global.ANIMATOR_DURATION_SCALE, 0);
于 2019-07-17T09:10:02.957 回答