Fragment standard transition not animating

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

I'm using the v4 android compatibility library to develop a tablet UI using fragments specifically for Android 2.2 devices and up.

Everything is working as it should, except that I can't get any animations to work, not even the standard animations.

Code:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);     ABCFragment abcFragment = new ABCFragment();     ft.replace(R.id.main_frame_layout_fragment_holder,abcFragment);          ft.addToBackStack(null);     ft.commit(); 

Instead of using a transit animation, the fragment freezes for about a second and the just disappears and the new one appears.

Using:

ft.setCustomAnimations(android.R.anim.slide_in_left,android.R.anim.slide_out_right); 

doesn't work either.

XML:

I read that the custom animation were broken in the compatibility library, but no one seems to be having issues with the standard transitions. I've tested this on a 3.2.1 Motorola Xoom, 2.3 Galaxy Tab 7", 2.2 emulator, and even on a HTC G2 with 2.3.4.

What could be wrong here?

回答1:

I finally got this to work after much trial and error.

First and foremost, get the lastest ACL, it did fix custom animations, and while this wasnt my exact problem, once those worked I ended up using them instead of standard transitions.

Right now im using:

ft.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out,android.R.anim.fade_in,android.R.anim.fade_out); 

The key to making it work on both Android 2.1, 2.2 and 2.3, as well as Android 3.0+ was to do the following:

  • Compile using Android 3.0.
  • In the manifest file, set android:hardwareAccelerated="true" inside your application tag.

Fragment animations now work on all devices. If you dont set the extra info in the application tag, the animation will ocurr, but in a very very choppy way, making it seem it didnt happen at all.

Hope this helps someone in the future!

As a note, there are some API checking tools so you are sure you arent using any APIs that arent available to you. I prefer to work on 2.1 so the IDE doesnt show anything I cant use, once I have stable code I jump back to compiling on 3.0



回答2:

Try getting the most recent ACL again, they have fixed it: http://code.google.com/p/android/issues/detail?id=15623#c19

Also I noticed that for setCustomAnimations, it needs to be set before transaction calls like replace in order to take effect.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.setCustomAnimations(R.anim.in_from_left, R.anim.out_to_right, R.anim.in_from_right, R.anim.out_to_left); ft.replace(android.R.id.content, newFrag); ft.addToBackStack(null); ft.commit(); 


回答3:

I've added NineOldAndroids support to the Google Support library. See http://www.github.com/kedzie/Support_v4_NineOldAndroids for details. It allows using Property Animations for Fragment Transitions, PageTransformers, and some other stuff.



回答4:

to perform top_to_bottom animation for fragment,

follow same to do top to bottom

FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.setCustomAnimations(R.anim.top_to_bottom_fragment, android.R.animator.fade_out); ft.replace(R.id.simple_fragment, fragment);  ft.commit(); 

top_to_bottom_fragment.xml

where valueFrom="-800" indicate bottom of your fragment layout.



回答5:

You must call setCustomAnimations before you add the fragment. This allows adding multiple fragments with different animations.



回答6:

Hope this helps someone. The API docs say use objectAnimator for fragment animations, but even with latest compatibility package objectAnimator in xml wasn't accepted by compiler.

This works for me:

For Android 3.0 or higher: declare xml objectAnimator in res/animator folder.

With Compatibility package for less than 3.0: declare xml animation in res/anim folder.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!