片段标准过渡没有动画
我正在使用 v4 android 兼容性库来开发平板电脑 UI,其中使用专门针对 Android 2.2 设备及更高版本的片段。
一切都按预期工作,除了我无法让任何动画工作,甚至标准动画也无法工作。
代码:
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();
片段没有使用过境动画,而是冻结了大约一秒钟,然后消失了,新的出现了。
使用:
ft.setCustomAnimations(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
也不起作用。
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.synergygb.mycustomapp"
android:id="@+id/LinearLayout01" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:gravity="bottom">
<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/main_frame_layout_fragment_holder">
</FrameLayout>
<!-- OTHER FIXED UI ELEMENTS-->
</RelativeLayout>
我读到自定义动画在兼容性库中被破坏,但似乎没有人对标准过渡有问题。我已经在 3.2.1 Motorola Xoom、2.3 Galaxy Tab 7"、2.2 模拟器上进行了测试,甚至在带有 2.3.4 的 HTC G2 上进行了测试。
这里可能出了什么问题?
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:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.synergygb.mycustomapp"
android:id="@+id/LinearLayout01" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:gravity="bottom">
<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/main_frame_layout_fragment_holder">
</FrameLayout>
<!-- OTHER FIXED UI ELEMENTS-->
</RelativeLayout>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
经过多次尝试和错误,我终于让它发挥作用。
首先也是最重要的,获取最新的 ACL,它确实修复了自定义动画,虽然这不是我的确切问题,但一旦这些工作成功,我最终会使用它们而不是标准过渡。
现在我正在使用:
使其在 Android 2.1、2.2 和 2.3 以及 Android 3.0+ 上工作的关键是执行以下操作:
android:hardwareAccelerated="true"
。片段动画现在适用于所有设备。如果您没有在应用程序标签中设置额外的信息,动画将会发生,但是以一种非常非常不稳定的方式,使它看起来好像根本没有发生。
希望这对将来的人有帮助!
请注意,有一些 API 检查工具,因此您可以确保没有使用任何您无法使用的 API。我更喜欢在 2.1 上工作,因此 IDE 不会显示任何我无法使用的内容,一旦我有了稳定的代码,我就会跳回到 3.0 上进行编译
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 was not my exact problem, once those worked I ended up using them instead of standard transitions.
Right now I am using:
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:
android:hardwareAccelerated="true"
inside your application tag.Fragment animations now work on all devices. If you do not set the extra info in the application tag, the animation will occur, but in a very very choppy way, making it seem as though it did not happen at all.
Hope this helps someone in the future!
As a note, there are some API checking tools so you are sure you are not using any APIs that aren't available to you. I prefer to work on 2.1 so the IDE doesn't show anything I can't use, once I have stable code I jump back to compiling on 3.0
尝试再次获取最新的 ACL,他们已经修复了它:
http://code.google.com/p/android/issues/detail?id= 15623#c19
我还注意到,对于setCustomAnimations,需要在replace之类的事务调用之前设置才能生效。
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.
在添加片段之前,您必须调用
setCustomAnimations
。这允许添加具有不同动画的多个片段。You must call
setCustomAnimations
before you add the fragment. This allows adding multiple fragments with different animations.要为片段执行从上到下的动画,
请按照相同的方式执行从上到下的
top_to_bottom_fragment.xml
,其中
valueFrom="-800"
指示片段布局的底部。to perform top_to_bottom animation for fragment,
follow same to do top to bottom
top_to_bottom_fragment.xml
where
valueFrom="-800"
indicate bottom of your fragment layout.我已将 NineOldAndroids 支持添加到 Google 支持库中。有关详细信息,请参阅 http://www.github.com/kedzie/Support_v4_NineOldAndroids。它允许使用片段转换、页面转换器和其他一些东西的属性动画。
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.
希望这对某人有帮助。 API 文档说使用 objectAnimator 进行片段动画,但即使使用最新的兼容包,xml 中的 objectAnimator 也未被编译器接受。
这对我有用:
对于 Android 3.0 或更高版本:在 res/animator 文件夹中声明 xml objectAnimator。
3.0以下的兼容包:在res/anim文件夹中声明xml动画。
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.