1

I am using slide animation when transaction replacing fragments

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager
            .beginTransaction();
    transaction.setCustomAnimations(
            R.anim.anim_from_left, R.anim.anim_to_right);
    transaction.replace(R.id.frame_drugstore, fragment).commit();

Animation for fragment which comes from left:

<?xml version="1.0" encoding="utf-8"?>
<set>

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:propertyName="translationX"
        android:valueType="floatType"
        android:valueFrom="-720"
        android:valueTo="0" />

</set>

Animation for fragment which to the right:

<?xml version="1.0" encoding="utf-8"?>
<set>

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:propertyName="translationX"
        android:valueType="floatType"
        android:valueFrom="0"
        android:valueTo="720" />

</set>

On my Galaxy Nexus (1280×720) this is working as needed. I am thinking about devices which have no 720px width screen.

How to set objectAnimator's valueFrom and valueTo according to screen size?

Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109

3 Answers3

-1

Get screen dimensions programatically

Display display = ((Activity) yourContext).getWindowManager ().getDefaultDisplay ();
            Point size = new Point ();
            display.getSize (size);
            int screenWidth = size.x;
            int screenHeight = size.y;
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
-1

As you need to readily alter values in the ObjectAnimator, I think you're better off creating it programmatically with something like:

ObjectAnimator oa = ObjectAnimator.ofFloat(menuFragmentContainer, "xFraction", 0, 0.25f);

This line is borrowed from here.

There are numerous ways to get the device's screen width but the most straightforward method is detailed here

Community
  • 1
  • 1
JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
  • This will not work for the above solution as it is trying to set custom animations in the fragment transaction. This can only be set using animation resources. – Steven Pena Sep 14 '16 at 02:57
-1

I think translate animation is better for your occasion. You can try the following translate animation:

This is for left to right animation:

<?xml version="1.0" encoding="utf-8"?>
<set>
  <translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXDelta="-100%"
   android:toXDelta="0"
   android:interpolator="@android:anim/accelerate_decelerate_interpolator"
   android:duration="1000"/>
</set>

This is for right to left animation:

<?xml version="1.0" encoding="utf-8"?>
<set>
  <translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXDelta="0"
   android:toXDelta="100%"
   android:interpolator="@android:anim/accelerate_decelerate_interpolator"
   android:duration="1000"/>
</set>
Lei Guo
  • 2,550
  • 1
  • 17
  • 22
  • 1
    But this solution is giving this error: java.lang.RuntimeException: Unknown animator name: translate – Joe Rakhimov Dec 08 '14 at 11:28
  • @JoeRichard That's really strange. I use translate animation in my app for fragment animation and they work well. Perhaps you can refer to this post:http://stackoverflow.com/a/20314517/1297670 , see the last answer. – Lei Guo Dec 08 '14 at 11:37
  • Ok, I think you just need to move those two animation to res/anim folder and have a try if they are at res/animator currently. – Lei Guo Dec 08 '14 at 11:42
  • This is path for animation: /res/anim/anim_from_left.xml Getting this error: 12-08 16:42:15.710: E/AndroidRuntime(32411): FATAL EXCEPTION: main 12-08 16:42:15.710: E/AndroidRuntime(32411): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.globalsolutions.medapp/com.globalsolutions.medapp.MainActivity}: java.lang.RuntimeException: Unknown animator name: translate – Joe Rakhimov Dec 08 '14 at 11:48
  • After checking the source code of support v4 fragments and the platform fragments (Api 19), I'm sure the problem can be solved by using support v4 fragments. – Lei Guo Dec 09 '14 at 08:59