46

我正在使用 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 摩托罗拉 Xoom、2.3 Galaxy Tab 7"、2.2 模拟器,甚至是 2.3.4 的 HTC G2 上进行了测试。

这里有什么问题?

4

6 回答 6

38

经过多次试验和错误,我终于得到了这个工作。

首先,获得最新的 ACL,它确实修复了自定义动画,虽然这不是我的确切问题,但一旦这些工作我最终使用它们而不是标准转换。

现在我正在使用:

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

使其在 Android 2.1、2.2 和 2.3 以及 Android 3.0+ 上运行的关键是执行以下操作:

  • 确保您使用的 API 仅适用于您希望支持的最低 API 级别(在我的情况下为 2.1)。
  • 使用 Android 3.0 编译。
  • 在清单文件中,android:hardwareAccelerated="true"在您的应用程序标签内设置。

片段动画现在适用于所有设备。如果你没有在应用程序标签中设置额外的信息,动画将会发生,但是以非常非常不稳定的方式,让它看起来好像根本没有发生。

希望这对将来的人有所帮助!

请注意,有一些 API 检查工具,因此您可以确定您没有使用任何您无法使用的 API。我更喜欢在 2.1 上工作,所以 IDE 不会显示任何我不能使用的东西,一旦我有稳定的代码,我就会跳回在 3.0 上编译

于 2011-10-25T16:28:19.550 回答
35

尝试再次获取最新的 ACL,他们已修复它: http ://code.google.com/p/android/issues/detail?id=15623#c19

我还注意到,对于 setCustomAnimations,它需要在诸如替换之类的事务调用之前设置才能生效。

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();
于 2011-10-24T23:41:31.467 回答
5

您必须在添加片段setCustomAnimations 之前调用。这允许添加具有不同动画的多个片段。

于 2015-06-04T04:10:13.747 回答
4

为片段执行 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

<objectAnimator android:duration="400" android:valueFrom="-800"
    android:valueTo="0" android:propertyName="y"
    android:valueType="floatType"
    xmlns:android="http://schemas.android.com/apk/res/android" />

其中valueFrom="-800"指示片段布局的底部。

于 2012-11-27T11:11:10.180 回答
3

我已将 NineOldAndroids 支持添加到 Google 支持库。有关详细信息,请参阅http://www.github.com/kedzie/Support_v4_NineOldAndroids。它允许将属性动画用于片段转换、PageTransformers 和其他一些东西。

于 2013-05-27T03:33:45.160 回答
0

希望这可以帮助某人。API 文档说将 objectAnimator 用于片段动画,但即使使用 xml 中的最新兼容包 objectAnimator 也未被编译器接受。

这对我有用:

对于 Android 3.0 或更高版本:在 res/animator 文件夹中声明 xml objectAnimator。

使用小于 3.0 的兼容包:在 res/anim 文件夹中声明 xml 动画。

于 2012-04-12T03:53:59.353 回答