Android objectAnimator动画真的很慢

时间:2016-07-12 01:09:58

标签: android android-fragments animation genymotion objectanimator

起初,我认为我的动画根本不起作用,因为新的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 :(得分:1)

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

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

一个简单但可怕的解决方案是将属性设置为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)。效果很棒!