【问题标题】:Ignore Developer's Settings Animation Scale in AndroidAndroid中忽略开发者设置动画比例
【发布时间】:2020-10-06 12:47:30
【问题描述】:

我的应用严重依赖动画来工作,当开发者的选项中改变它们的速度时,整个应用会中断并停止工作。我一直在研究忽略开发人员设置并强制动画按应有方式运行的方法,但它并没有真正改变任何东西。

我在我的应用程序类中调用这个函数onCreate

private fun fixAnimation() {
    val durationScale = Settings.Global.getFloat(contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f)
    Log.d("fixAnimation", "durationScale: $durationScale")
    if (durationScale != 1f) {
        try {
            ValueAnimator::class.java.getMethod("setDurationScale",Float::class.javaPrimitiveType).invoke(null, 1f)
        } catch (t: Throwable) {
            Log.e("fixAnimation", t.message ?: t.toString())
        }
    }
}

但什么都没有改变。

从这里得到它:Make ObjectAnimator animation duration independent of global animator duration scale setting in developer options 在这里:https://medium.com/@arpytoth/android-objectanimator-independent-of-global-animator-duration-fe33c808c83e 显然它适用于某些人,但不适用于我的情况。

可能是因为我使用的是 ViewPropertyAnimator,而不是直接使用 ValueAnimator?但它应该仍然有效,因为这会全局更改 ValueAnimator 的持续时间比例设置,并且 ViewPropertyAnimator 由 ValueAnimator 支持...

【问题讨论】:

    标签: android kotlin reflection android-animation android-reflection


    【解决方案1】:

    你调用你的代码太早了。

    如果您在ValueAnimator.setDurationScale(float durationScale) 中设置断点,您会注意到在您的代码运行后WindowManagerGlobal 会将持续时间比例重置回系统值。

    因此,一种可能的解决方案是在Activity.onResume() 中调用您的代码。从理论上讲,您也可以将其放入Activity.onCreate(),但是如果用户(或系统,例如为了节省电池)然后在应用程序处于后台时更改它,它将无法工作。

    仍然存在边缘情况,例如如果系统决定在应用程序处于前台或可能处于多窗口场景时更改因子,但这与此解决方法一样好。毕竟,您可能不想每隔一秒左右检查和修复这个问题,因为这涉及到反射。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    相关资源
    最近更新 更多