0

I am facing problem while reading Animation scale duration flag in Marshmallow. Same code is working in earlier version of Marshmallow (6.0.0).

I am using android.app.Fragment.setCustomTransition() method for fragment animation. When Animation scale duration is 0 in developer option settings, fragments do not get displayed. So i had to disable animation over this condition.

My code snippet:

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;
    }

The problem is :this code is returning true on each invoke even if animation scale duration is not 0.

Has anyone faced this issue?

Edit1

Following is the code snippet where I use isAnimationOff() method to load a fragment:

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();
}

Edit2

Here is the custom LinearLayout that has custom property:

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();
    }
}
Sachin Chandil
  • 17,133
  • 8
  • 47
  • 65
  • what actually do you need `isAnimationOff` method for? – pskink Apr 15 '16 at 14:03
  • i check if animation scale is 0(animation is off from developer option settings). if no `setCustomTransition()` is invoked else not invoked. – Sachin Chandil Apr 16 '16 at 07:25
  • actualy i dont understand your goal: there is no such method `Fragment.setCustomTransition()` do you mean `FragmentTransaction.setTransition()`? – pskink Apr 20 '16 at 06:49
  • @pskink my bad, its `setCustomAnimations()`. Check edited ans. – Sachin Chandil Apr 20 '16 at 08:53
  • so you are suggesting if some app uses `setCustomAnimations` and `ANIMATOR_DURATION_SCALE` is set to 0 no fragments are shown? – pskink Apr 20 '16 at 09:10
  • Not particularly, If you are using custom property animation then fragment is not shown. I am using left to right and right to left animation. So for keeping track of with of All devices dimension. I had to create a property `FractionTranslationX` that lets animator decide how much `X` value to measure. – Sachin Chandil Apr 20 '16 at 09:22
  • so post it, this is a root of your problems – pskink Apr 20 '16 at 09:23
  • Actually code is working fine in all earlier version of Android than Marshmallow. – Sachin Chandil Apr 20 '16 at 09:24
  • @pskink i am unable to read `ANIMATOR_DURATION_SCALE ` flag as each time it gives default value that is 0. – Sachin Chandil Apr 20 '16 at 09:25
  • @pskink i have edited my que also. – Sachin Chandil Apr 20 '16 at 09:28
  • and you are using `` with `propertyName="fractionTranslationX"` and `"fractionTranslationY"`, right? so add some `Log.d` inside `setFractionTranslationX` and `setFractionTranslationY` and see with what params they are called (or rather with what values `super.setTranslationX` and `super.setTranslationY` are called by you) – pskink Apr 20 '16 at 09:58
  • @pskink I have already checked. On Setting `ANIMATOR_DURATION_SCALE` to zero, these methods do not get called as animation is disabled. – Sachin Chandil Apr 20 '16 at 10:18
  • see http://pastebin.com/rc67zjX8 – pskink Apr 20 '16 at 10:36
  • hey @pskink i did not get what u want to say in this link. but as u suggested to debug that `setFractionTranslationX` is getting called or not, i double checked here and found an issue that i had in my code. Thanks for your help. Now there is no need to check if animation is off or not. fragment is visible in both the cases. – Sachin Chandil Apr 20 '16 at 11:22
  • so why did you say that `"I have already checked [...] these methods do not get called as animation is disabled"`? – pskink Apr 20 '16 at 11:24
  • I was wrong here. That is what i figured out when this code wasn't working so i just fired that statement. But when i checked it again then i knew. thanks BTW. – Sachin Chandil Apr 20 '16 at 11:33

1 Answers1

0

I had an issue in my FractionLinearLayout class. In set setFractionTranslationX() method, getWidth() was returning 0 at the very first call. That led my code to prevent Fragment to appear when animation is off. Now i have replaced getWidth() line with getResources().getDisplayMetrics().widthPixels and everything is working fine.

Updated Code:

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();
    }
}

But Still I don't knowl, why is ANIMATOR_DURATION_SCALE flag not working in Marshmallow.

Sachin Chandil
  • 17,133
  • 8
  • 47
  • 65