Skip to content

Instantly share code, notes, and snippets.

@zerobranch
Created October 4, 2018 08:23
Show Gist options
  • Save zerobranch/8c2f4f1dc8ae8283cba2474d13ed9428 to your computer and use it in GitHub Desktop.
Save zerobranch/8c2f4f1dc8ae8283cba2474d13ed9428 to your computer and use it in GitHub Desktop.
When animating, the next fragment will be ABOVE the previous one. Copy xml resources to res/animator
public abstract class BaseMainFragment extends Fragment {
@Override
public Animation onCreateAnimation(int transit, final boolean enter, int nextAnim) {
if (nextAnim == R.animator.next_fragment_anim) {
Animation nextAnimation = AnimationUtils.loadAnimation(getContext(), nextAnim);
nextAnimation.setAnimationListener(new Animation.AnimationListener() {
private float startZ = 0f;
@Override
public void onAnimationStart(Animation animation) {
if (getView() != null) {
startZ = ViewCompat.getTranslationZ(getView());
ViewCompat.setTranslationZ(getView(), 1f);
}
}
@Override
public void onAnimationEnd(Animation animation) {
if (getView() != null) {
getView().postDelayed(() -> {
if (getView() != null) {
ViewCompat.setTranslationZ(getView(), startZ);
}
}, 100);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
return nextAnimation;
} else {
return null;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:duration="150"
android:fromXDelta="0%"
android:toXDelta="-10%"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:duration="150"
android:fromXDelta="0%"
android:toXDelta="100%"/>
</set>
public class FragmentNavigator {
private void setFragmentAnimation(FragmentManager fragmentManager) {
fragmentManager.beginTransaction()
.setCustomAnimations(
R.animator.next_fragment_anim, R.animator.current_fragment_anim,
R.animator.next_fragment_anim_back, R.animator.current_fragment_anim_back)
.replace(containerId, fragment, screenKey)
.addToBackStack(command.screenKey)
.commit();
}
}
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:duration="150"
android:fromXDelta="100%"
android:toXDelta="0%"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:duration="150"
android:fromXDelta="-10%"
android:toXDelta="0%"/>
</set>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment