I've been trying to add some cool fragment transition animations
for my application lately, and I more or less have figured out how to do so. However I can only get my animations to work on API level 11 and up
. This is because I am using Property Animations which isnt supported for the compat libraries. I tried to resolve this issue by including the nineOldAndroids
library. I also threw in the support-v4-NineOldAndroids
library to help.
Still my animations work when I run a newer device and throw runtime errors for my legacy devices. Is SupportFragment animation possible? If so, how do you suggest it should be done?
This is my logic for animating my fragments. I need to have my main fragment slide off to the right but remain in view while my navigation fragment slides in from the left.
public void flipFragment() {
if(showingMenu){
getSupportFragmentManager().popBackStack();
slideToMain(null);
return;
}
showingMenu = true;
Animator.AnimatorListener listener = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in, 0, 0, R.anim.slide_out)
.add(R.id.fragment_container, customNavigation)
.addToBackStack(null)
.commit();
}
};
slideToNav(listener);
}
private void slideToMain(String sReddit) {
if (sReddit != null && apiTask != null) {
apiTask.execute(sReddit);
}
View movingFragmentView = mainView.getView();
PropertyValuesHolder rotateY = PropertyValuesHolder.ofFloat("rotationY",-15f, 0f);
PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX",1.0f);
PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
PropertyValuesHolder translateX = PropertyValuesHolder.ofFloat("translationX",0f);
ObjectAnimator movingFragmentsAnimator = ObjectAnimator.ofPropertyValuesHolder(movingFragmentView, rotateY, scaleX, scaleY, translateX);
movingFragmentsAnimator.setDuration(1300);
movingFragmentsAnimator.setStartDelay(1);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(movingFragmentsAnimator);
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
showingMenu = false;
}
});
animatorSet.start();
}
private void slideToNav(Animator.AnimatorListener listener) {
View movingFragmentView = mainView.getView();
PropertyValuesHolder rotateY = PropertyValuesHolder.ofFloat("rotationY",-15f);
PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX",0.8f);
PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.8f);
PropertyValuesHolder translateX = PropertyValuesHolder.ofFloat("translationX",400f);
ObjectAnimator movingFragmentsAnimator = ObjectAnimator.ofPropertyValuesHolder(movingFragmentView, rotateY, scaleX, scaleY, translateX);
movingFragmentsAnimator.setDuration(1000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(movingFragmentsAnimator);
animatorSet.addListener(listener);
animatorSet.start();
}
Animation xml
<set>
<objectAnimator
android:valueFrom="-100"
android:valueTo="-1290"
android:propertyName="translationX"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="1300"
/>
<objectAnimator
android:valueFrom="0"
android:valueTo="1"
android:propertyName="alpha"
android:startOffset="1000"
android:duration="1"
/>
</set>
Gradle Dependencies
compile files('libs/nineoldandroids-2.4.0.jar')
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
compile files('libs/support-v4-animator-13.0.0-sources.jar')
compile "com.android.support:appcompat-v7:19.1.0+"