Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@godaikun_method

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

簡単に言えば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);
    }
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
2
Help us understand the problem. What are the problem?