1

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+"
feilong
  • 1,564
  • 3
  • 16
  • 22
  • You can also try [Nine Old Android](http://nineoldandroids.com/) – Nadeem Iqbal Aug 20 '14 at 07:17
  • Thanks for your suggestion, but if you read my post you would have noted that the question is about getting nineoldandroids to work for fragment animation – feilong Aug 20 '14 at 12:42

1 Answers1

0

Take a look at this post - its about Flip animation. Also this post must be helpful, at least I succeeded with Flip animation for API level prior to 11 (actually tested on 2.3.5 and it works). In short - you need a "support-v4 support for NineOldAndroids" lib.

Community
  • 1
  • 1
Stan
  • 6,511
  • 8
  • 55
  • 87
  • 1
    Were you able to do this in android studio? I'm running into all types of problems setting up support-v4-animator. Do i just need it and nineoldandroids as my dependencies or do I need to use actionbarsherlock as well? – feilong Aug 20 '14 at 13:18
  • Actually I did it with Ecplise based ADT and I'm not that good in Android Studio to help you with it. I guess you should dl "support-v4 support for NineOldAndroids" lib and extract and include it in the your Android Studio project. – Stan Aug 20 '14 at 13:21
  • I think I've managed to set it up however I am getting a null pointer exception for animatorSet.Start() when ever I try the animation in a 2.3.7 device but it works fine for my 3.0> devices. – feilong Aug 20 '14 at 14:42
  • which means you did not managed to set it up properly :) Try to import module in studio pointing to the src of "support-v4 support for NineOldAndroids". then you should to add in your app's build.gradle line like compile project(':here is the folder of support-v4 support for NineOldAndroids') – Stan Aug 20 '14 at 15:16
  • I've been using the support-v4-animator-r12.jar because when I tried to import the project into android studio I got all types of gradle errors pointing to the getAndroidPrebuilt commands in the library. So gradle refuses to build the project. I think the jar should be working though since I have removed my android v4 support lib dependencies and my supportFragments are still resolving – feilong Aug 20 '14 at 15:35