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