Android动画一:Activity过渡动画详细实现原理

虽然 Android 5.0 以后推出了新的过渡动画方式,但一般只是用于特定的场合使用,activity.overridePendingTransition() 通用方式的过渡动画仍是很经常使用。 java

原理分析

startActivity(Intent(this,SecondActivity::class.java))
overridePendingTransition(enterAnim, exitAnim)
复制代码

overridePendingTransition有两个参数,第一个参数(enterAnim)是做用于SecondActivity 的进入屏幕可见区域效果,第二个参数(exitAnim)是做用于当前 Activity 离开屏幕可见区域效果。android

示意图

RightIn:从右边滑入屏幕(iOS默认效果)

iOS 默认的效果,新的Activity从右边(R)进入显示区域,当前Activity从左边离开显示区域到(L)。git

enterAnim(activity_right_to_left_enter.xml):X轴从 100% 到 0github

<?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" />
</set>
复制代码

exitAnim(activity_right_to_left_exit.xml):X轴从 0 到 -100%bash

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

使用ide

startActivity(Intent(this,SecondActivity::class.java))
overridePendingTransition(R.anim.activity_right_to_left_enter, R.anim.activity_right_to_left_exit)
复制代码
BottomIn:从底部弹出Activity(经常使用效果)

通常从底部弹出新Activity,就是从B区域到屏幕可见区域,当前的Activity是保持不变的。 enterAnim(activity_bottom_to_top_enter.xml):Y轴从 100% 到 0动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="300"
        android:fromYDelta="100%"
        android:toYDelta="0" />
</set>
复制代码

exitAnim(no_anim.xml):Y轴保持不变ui

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="300"
        android:fromYDelta="0"
        android:toYDelta="0"/>
</set>
复制代码

使用this

startActivity(Intent(this,SecondActivity::class.java))
overridePendingTransition(R.anim.activity_bottom_to_top_enter, R.anim.no_anim)
复制代码

RightOut(和RightIn对应,iOS 默认效果)

前面讲了startActivity的转场动画,下面讲finish()的转场动画。overridePendingTransition有两个参数,第一个参数(enterAnim)是做用于上一个Activity的进入屏幕可见区域效果,第二个参数(exitAnim)是做用于当前 Activity 离开屏幕可见区域效果。spa

iOS默认的finish动画,是当前的Activity从屏幕可见区域到R区域,上一个Activity从L区域到屏幕可见区域。

enterAnim(activity_left_to_right_enter.xml):X轴从 -100% 到 0

<?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" />
</set>
复制代码

exitAnim(activity_left_to_right_exit.xml):X轴从 0 到 100%

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

使用

finish()
overridePendingTransition(R.anim.activity_left_to_right_enter, R.anim.activity_left_to_right_exit)
复制代码
BottomOut(和BottomIn对应,经常使用效果)

从屏幕底部滑出效果是,当前Activity从底部滑出屏幕可见区域,上一个Activity保持不变,和BottomIn不一样的是,enterAnim是不须要使用动画,由于上一个Activity已经在屏幕的后面了,只须要改变当前Activity消失的效果。 exitAnim(activity_top_to_bottom_exit.xml):Y轴从 0 到 100%

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="300"
        android:fromYDelta="0%"
        android:toYDelta="100%" />
</set>
复制代码

使用

finish()
overridePendingTransition(0, R.anim.activity_top_to_bottom_exit)
复制代码

完整示例代码

github.com/taoweiji/Ac…

相关文章
相关标签/搜索