2013-09-27 117 views
3

我已經使用Activity支持-v4庫中最近支持的ActivityOptionsCompat和ActivityCompat實現了窗口動畫。ActivityOptionsCompat不工作在android 2.3

我用下面的代碼實現的動畫:

Intent subActivity = new Intent(WindowAnimations.this, 
        AnimatedSubActivity.class); 
      // The enter/exit animations for the two activities are 
      // specified by xml resources 
      Bundle translateBundle = ActivityOptionsCompat 
        .makeCustomAnimation(WindowAnimations.this, 
          R.anim.slide_in_left, R.anim.slide_out_left) 
        .toBundle(); 
      ActivityCompat.startActivity(WindowAnimations.this, 
         subActivity, translateBundle); 

上面的代碼是在Android 4.3及4.0工作正常。

任何人都可以使它在2.X工作?任何幫助表示讚賞。

+0

從'startActivity的文檔()'方法:*當上平臺,此功能不存在活動將正常推出的版本上運行* 。 – Luksprog

+0

感謝您的快速評論。但正如我可以看到在ActivityCompat文檔http://developer.android.com/reference/android/support/v4/app/ActivityCompat.html我無法找到任何描述這種描述there.Also我認爲因爲它支持v-4 lib以支持API級別4的舊版本的動畫。請給出您的想法嗎? –

+0

我引用的文檔是採用3個參數(Activity,Intent,Bundle)的'startActivity()'方法。我沒有使用該兼容類,但可以合理地假設較低版本的動畫可能不可行(由於系統本身的限制)。該類也可能在兼容包中引入,因此您仍然可以在可能的情況下使用動畫(在較低版本中回落到默認值),因此開發人員不必實現基於API的邏輯。 – Luksprog

回答

10

@Luksprog基本上已經在你的問題的第一個問題上回答了這個問題,但是這裏有更多的文檔來進一步支持這個說法。

首先,仔細閱讀javadoc的ActivityCompat#startActivity(Activity activity, Intent intent, Bundle options)。我已經強調了一些相關的關鍵字:

額外的發射信息如果能夠開始活動。

在Android 4.1+中引入了其他選項,以允許更多 控制活動啓動動畫。當 可用時,應用程序可以使用此方法與ActivityOptionsCompat一起使用這些動畫。 當在不存在此功能 的平臺版本上運行時,活動將正常啓動

因此,基本上告訴我們,(可選)動畫功能只適用於本機支持它的Android版本。在所有其他平臺版本上,Activity將「正常」啓動 - 即沒有可選動畫。

的實際證明可以很容易地在source code of ActivityCompat找到:

public static void startActivity(Activity activity, Intent intent, Bundle options) { 
    if (Build.VERSION.SDK_INT >= 16) { 
     ActivityCompatJB.startActivity(activity, intent, options); 
    } else { 
     activity.startActivity(intent); 
    } 
} 

換句話說:如果此碼是一個預先JB設備上執行,一個普通的舊startActivity()呼叫會狂,其中options參數被忽略。只有JB設備最終會使用它。

也許多餘的提,但顯然同樣適用於對應的startActivityForResult()。總結:目前支持庫只提供一個靜態幫助類來執行「向後兼容的時尚」的某些功能 - 它實際上並未反向支持該功能(尚未)。所有它在這個階段所做的就是不必在自己的應用程序中編寫if/else條件。

這就是說,目前的實現確實允許將來的實際功能回溯。這也可能是爲什麼ActivityOptionsCompat類存在。目前,該課程爲JB之前的設備提供了一個「空白」的實現,理論上講,它可以在後期「填補」。通過這些兼容性幫助程序調用的任何代碼都將自動開始工作。

Example of an ActivityOptionsCompat call返回一個空的實現:

public static ActivityOptionsCompat makeCustomAnimation(Context context, 
     int enterResId, int exitResId) { 
    if (Build.VERSION.SDK_INT >= 16) { 
     return new ActivityOptionsImplJB(
      ActivityOptionsCompatJB.makeCustomAnimation(context, enterResId, exitResId)); 
    } 
    return new ActivityOptionsCompat(); 
} 
相關問題