首页 文章

Android objectAnimator动画真的很慢

提问于
浏览
2

起初,我认为我的动画根本不起作用,因为新的Fragment ViewGroup只会在一帧中显示在屏幕上 . 然后我放慢了动画的速度,所以它需要3秒(3000毫秒),并发现它只是发生在如此大的块中,而这一切都发生在一帧中 . 当我将它增加到3秒时,它每秒大约增加4帧,这非常糟糕 . 我正在运行Genymotion仿真器 . 例如,当我点击“设置”应用时,模拟器看起来非常快 .

我刚开始在这个应用程序上开发,所以它没有做任何事情 . 到目前为止,它主要只是一个shell,我正试图在屏幕上设置一个新的Fragment .

新的ViewGroup是一个名为SlideableLayout的自定义类,它提供了以下属性:

public float getXFraction() {
    final int width = getWidth();
    if (width != 0) {
        return getX() / getWidth();
    } else {
        return getX();
    }
}

public void setXFraction(float xFraction) {
    Log.d("SL", "setting xFraction="+xFraction);
    final int width = getWidth();
    if (width > 0) {
        setX(xFraction * width);
    } else {
        setX(-10000);
    }
}

然后我像这样添加片段:

getFragmentManager()
    .beginTransaction()
    .setCustomAnimations(R.animator.slide_in_from_right, R.animator.slide_out_to_the_left,
            R.animator.slide_in_from_the_left, R.animator.slide_out_to_the_right)
    .add(R.id.navrootlayout, fragment)
    .addToBackStack(null)
    .commit();

R.animator.slide_in_from_right 动画如下所示:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:interpolator="@interpolator/decelerate_cubic"
        android:valueFrom="1"
        android:valueTo="0"
        android:valueType="floatType"
        android:propertyName="xFraction"
        android:duration="@integer/navigation_animation_duration"/>
</set>

因此,系统应该将 xFraction 属性从 1.0 动画到 0.0 ,并且应该非常快速地计算每个帧,因为它所做的只是得到 width 并乘以分数 .

我不确定为什么它以如此低的帧速率运行 . 我在物理设备上尝试过它很好 .

编辑:

我是否需要在Genymotion仿真器上设置某些配置选项才能使动画以正常的帧速率运行?

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

1 回答

  • 0

    显然,这是由xFraction和yFraction引起的,必须由两个片段的根视图实现:https://stackoverflow.com/a/25701362/2964379

    老实说,这只是一个史诗般的失败 . Google是否希望人们为了添加这两种方法而扩展每个视图?

    一个简单但可怕的解决方案是将属性设置为x或y并将值设置为等于或大于屏幕的大数,然后它可以快速工作,但根据值,动画中可能存在巨大的间隙 .

    最佳解决方案(来自https://stackoverflow.com/a/20480676/2964379)是将动画设置为x或y,但完全删除 valueFromvalueTo . 然后覆盖所有片段' onCreateAnimator (在自定义片段基类中),使用 AnimatorInflater.loadAnimator(activity, nextAnim) 获取动画集并在其子节点上调用 setFloatValues . 例如,您可以检查 nextAnim == R.animator.slide_in_from_right 并调用 setFloatValues(screenWidth, 0) . 效果很好!

相关问题