0

Animation scale duration在 Marshmallow 中阅读 flag 时遇到问题。相同的代码在早期版本的 Marshmallow (6.0.0) 中工作。

我正在使用android.app.Fragment.setCustomTransition()片段动画的方法。当动画比例持续时间为 0 时developer option settings,片段不会显示。所以我不得不在这种情况下禁用动画。

我的代码片段:

public static boolean isAnimationOff()
    {
        final float animatorSpeed;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        {
            animatorSpeed = Settings.Global.getFloat(
                    context.getContentResolver(),
                    Settings.Global.ANIMATOR_DURATION_SCALE,
                    0);
        }
        else
        {
            animatorSpeed = Settings.System.getFloat(
                    context.getContentResolver(),
                    Settings.System.ANIMATOR_DURATION_SCALE,
                    0);
        }
        return animatorSpeed == 0;
    }

true问题是:即使动画比例持续时间不是 0,此代码也会在每次调用时返回。

有没有人遇到过这个问题?

编辑1

以下是我使用isAnimationOff()方法加载片段的代码片段:

private void loadFragment(Fragment fragment)
{
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    if (!Constants.isAnimationOff())
        transaction.setCustomAnimations(animSlideIn, animSlideOut);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.frameTopContainer, fragment)
            .commitAllowingStateLoss();
}

编辑2

这是具有自定义属性的自定义 LinearLayout:

public class FractionLinearLayout extends LinearLayout
{

    DisplayMetrics matrics = getContext().getResources().getDisplayMetrics();

    public FractionLinearLayout(Context context, AttributeSet attrs,
                                int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
    }

    public FractionLinearLayout(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub

    }

    public FractionLinearLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public float getFractionTranslationX()
    {

        return getWidth() > 0 ? super.getTranslationX() / getWidth() : Float.MAX_VALUE;
    }

    public void setFractionTranslationX(float translationX)
    {

        int width = getWidth();
        super.setTranslationX(width > 0 ? width * translationX : Float.MAX_VALUE);
    }

    public float getFractionTranslationY()
    {

        return getHeight() > 0 ? super.getTranslationX() / getHeight() : Float.MAX_VALUE;
    }

    public void setFractionTranslationY(float translationY)
    {
        int height = getHeight();
        super.setTranslationY(height > 0 ? height * translationY : Float.MAX_VALUE);
    }

    public float getAnimWidth()
    {

        return getLayoutParams().width;
    }

    public void setAnimWidth(int animWidth)
    {

        getLayoutParams().width = animWidth;
        requestLayout();
    }
}
4

1 回答 1

0

我在FractionLinearLayout课堂上遇到了一个问题。在 set setFractionTranslationX()方法中,getWidth()在第一次调用时返回 0。这导致我的代码阻止动画关闭时出现片段。现在我已经替换getWidth()了线路,getResources().getDisplayMetrics().widthPixels一切正常。

更新代码:

public class FractionLinearLayout extends LinearLayout
{

    DisplayMetrics matrics = getContext().getResources().getDisplayMetrics();

    public FractionLinearLayout(Context context, AttributeSet attrs,
                                int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
    }

    public FractionLinearLayout(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub

    }

    public FractionLinearLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public float getFractionTranslationX()
    {

        return getWidth() > 0 ? super.getTranslationX() / getWidth() : 0;
    }

    public void setFractionTranslationX(float translationX)
    {
        int width = getResources().getDisplayMetrics().widthPixels;
        super.setTranslationX(width > 0 ? width * translationX : 0);
    }

    public float getFractionTranslationY()
    {

        return getHeight() > 0 ? super.getTranslationY() / getHeight() : 0;
    }

    public void setFractionTranslationY(float translationY)
    {
        int height = getResources().getDisplayMetrics().heightPixels;
        super.setTranslationY(height > 0 ? height * translationY : 0);
    }

    public float getAnimWidth()
    {

        return getLayoutParams().width;
    }

    public void setAnimWidth(int animWidth)
    {

        getLayoutParams().width = animWidth;
        requestLayout();
    }
}

但我仍然不知道,为什么ANIMATOR_DURATION_SCALE标志在棉花糖中不起作用。

于 2016-04-20T11:29:59.650 回答