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添加片段。這允許添加具有不同動畫的多個片段。