LoginSignup
3
2

More than 3 years have passed since last update.

【Kotlin】AndroidでiOSライクな画面遷移アニメーションを実装する

Last updated at Posted at 2020-09-10

簡単に言えばiOSでUINavigationControllershowするような画面遷移を、
Androidで簡単に実装出来ます。

手順1:res/animを作成する

スクリーンショット 2020-09-10 11.39.28.png

手順2:画像のようにxmlファイルを4件追加する

close_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%"
        android:toXDelta="0%"
        android:duration="300"
        android:fillAfter="true"
        android:fillEnabled="true"/>
</set>

close_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0%"
        android:toXDelta="100%"
        android:duration="300"
        android:fillAfter="true"
        android:fillEnabled="true"/>
</set>

open_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%"
        android:toXDelta="0%"
        android:duration="300"
        android:fillAfter="true"
        android:fillEnabled="true"/>
</set>

open_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0%"
        android:toXDelta="-100%"
        android:duration="300"
        android:fillAfter="true"
        android:fillEnabled="true"/>
</set>

手順3:アニメーションを行いたいA画面の画面遷移実行時に下記の記述を入れる。

val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
overridePendingTransition(R.anim.open_enter, R.anim.open_exit);

手順3:閉じるアニメーションを行いたいB画面の画面遷移実行時に下記の記述を入れる。

    override fun finish() {
        super.finish()
        overridePendingTransition(R.anim.close_enter, R.anim.close_exit);
    }
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2