42

我使用的V4的Android兼容性库开发使用片段专为Android 2.2设备和一个平板UI。片段标准过渡不动画

一切工作,因为它应该,但我不能让任何动画的工作,甚至没有标准的动画。

代码:

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。

出了什么问题吗?

+0

达尔维克(调试器)给你什么? – IAmYourFaja

+0

你有任何logcat输出给我们吗? –

+0

没什么,这就是为什么我输了。没有错误。 Logcat什么也没有显示。只需定期回调系统,与应用内更改无关。 检查mTransition的值= 4099. 该值由以下项设置: ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); – blindstuff

回答

35

我终于得到了这一点。经过多次试验和工作后才能工作RROR。

首先,获得最新的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,可以支持您希望支持的最低API级别(在我的案例2.1中)。
  • 使用Android 3.0进行编译。
  • 在清单文件中,在应用程序标签内设置android:hardwareAccelerated="true"。现在

动画片段在所有设备上工作。如果您不在应用程序标签中设置额外的信息,动画将会发生变化,但以非常非常不连贯的方式,使它看起来根本没有发生。

希望这可以帮助别人的未来!

作为一个说明,有一些API检查工具,让你确信你使用的arent的arent提供给你任何的API。我更喜欢2。1所以IDE不显示我不能使用的任何东西,一旦我有稳定的代码,我跳回编译3.0 3.0

+0

谢谢你的回答,我在SII,HTC evo,Nexus Galaxy和SIII上进行测试,看到Nexus滞后于动画是非常可怕的,硬件标签的一切行为都如预期:) – Goofyahead

+0

@bindinduff如果你不是使用API​​所支持的最低级别以上的任何代码,无需针对较新的API进行编译。编译对比较新的API的唯一方法是可以访问新的类和方法。 –

+2

您需要在3.0或更高版本中编译才能识别出'android:hardwareAccelerated =“true”'。 – blindstuff

32

尝试获得的最近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(); 
+0

我已经完成了你描述的两件事情,我曾在评论中声明要跟进我的问题。我已经开始工作了,但我只是在我给出一个答案之前进行测试,我不是100%确定的。我得到它与Galaxy Tab一起工作,在XOOM上,动画看起来像垃圾。 – blindstuff

+0

对不起,我无法评论其他人的帖子(没有足够的声望)。很高兴听到你解决它,但! – dvd

+0

感谢您的帮助,+1,使用'ft.setCustomAnimations(R.anim.in_from_left,R.anim.out_to_right,R.anim.in_from_right,R.anim.out_to_left);'代替只是'ft.setCustomAnimations(R.anim.in_from_left,R.anim.out_to_right);' – blindstuff

0

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

这个工作对我来说:

如果是Android 3.0或更高版本:申报RES /动画文件夹中的XML objectAnimator。

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

3

执行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"注明您的片段布局的底部。

2

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