Android:Fragmentの画面遷移時にアニメーション
Androidで、Fragmentの画面遷移の時にアニメーションを付けるやりかたを書きます。 FragmentTransaction.setCustomAnimationsを使いました。 FragmentTransaction | Android Developers
進む時のみにアニメーションを付ける場合
setCustomAnimations(int enter, int exit)
- enter: 次に表示されるFragmentのアニメーション
- exit: 今表示されているFragmentのアニメーション
Backボタンなど、戻る時にもアニメーションを付ける場合
setCustomAnimations(int enter, int exit, int popEnter, int popExit)
popEnter、popExitに戻る時のアニメーションを指定します。
- popEnter: 戻る時に次に表示されるFragmentのアニメーション
- popExit: 戻る時に今表示されているFragmentのアニメーション
引数で指定するのは、アニメーションを指定したリソースIDです。 左からスライド・インさせるアニメーションであればこんな感じ。
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromXDelta="-100%" android:toXDelta="0" /> <alpha android:duration="300" android:fromAlpha="0.0" android:toAlpha="1.0" /> </set>
Activityの画面遷移全体に付けるのなら、Themeに指定するのが一番楽です。 詳しくはこちらが参考になりました。 【Android】画面遷移時のアニメーション設定 - Furudateのブログ
参考にさせてもらったサイト
LollipopでFragmentをスライドインするアニメーションの適用 - Qiita
java.lang.RuntimeException: Unknown animator name: translate
が発生した時の対応を書いてありました。同じエラーが発生したのですが、自分の場合は、FragmentTransactionはSupportライブラリを使うことで、回避しました。