零、前言

1.打开RedActivity点击屏幕进入BlueActivity,此时RedActivity左移出,BlueActivity左移入
2.点击返回按钮BlueActivity右移出,RedActivity右移入

默认跳转模式:

Activity默认跳转动画.gif

本案例效果:

Activity跳转动画.gif

一、测试类

1.红色Activity:
public class RedActivity extends AppCompatActivity {

    @BindView(R.id.root)
    ConstraintLayout mRoot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color);
        ButterKnife.bind(this);
        mRoot.setBackgroundColor(Color.RED);
    }

    @OnClick(R.id.root)
    public void onViewClicked() {
        startActivity(new Intent(this, BlueActivity.class));
        overridePendingTransition(R.anim.open_enter_t,R.anim.open_exit_t);
    }
}
2.蓝色Activity
public class BlueActivity extends AppCompatActivity {

    @BindView(R.id.root)
    ConstraintLayout mRoot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color);
        ButterKnife.bind(this);
        mRoot.setBackgroundColor(Color.BLUE);
    }

    @OnClick(R.id.root)
    public void onViewClicked() {
        startActivity(new Intent(this, RedActivity.class));
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.close_enter_t, R.anim.close_exit_t);
    }
}

Activity跳转动画.png


二、动画

1.左移出:anim/open_exit_t.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/decelerate_interpolator">
    <!--左移出-->
    <translate
        android:duration="500"
        android:fromXDelta="0%p"
        android:toXDelta="-100%p"/>
</set>
2.左移入:anim/open_enter_t.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/decelerate_interpolator">
    <!--左移入-->
    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0%p"/>
</set>
3.右移出:anim/close_exit_t.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/decelerate_interpolator">
    <!--右移出-->
    <translate
        android:duration="500"
        android:fromXDelta="0%p"
        android:toXDelta="100%p"/>
</set>
4.右移入:anim/close_enter_t.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/decelerate_interpolator">
    <!--右移入-->
    <translate
        android:duration="500"
        android:fromXDelta="-100%p"
        android:toXDelta="0%p"/>
</set>
附录、布局文件:layout/activity_color.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    android:id="@+id/root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".animation.RedActivity">
</android.support.constraint.ConstraintLayout>

三、利用样式来改变Activity跳转动画

1.定义样式:values/styles.xml
    <style name="TranAnim_Activity"
           parent="@android:style/Animation.Activity">
        <item name="android:activityOpenEnterAnimation">@anim/open_enter_t</item>
        <item name="android:activityOpenExitAnimation">@anim/open_exit_t</item>
        <item name="android:activityCloseEnterAnimation">@anim/close_enter_t</item>
        <item name="android:activityCloseExitAnimation">@anim/close_exit_t</item>
    </style>

    <style name="AppThemeTranAnim" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowAnimationStyle">@style/TranAnim_Activity
        </item>
    </style>
2.使用样式:app/src/main/AndroidManifest.xml
<activity android:name=".animation.RedActivity"
          android:theme="@style/AppThemeTranAnim">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>
<activity android:name=".animation.BlueActivity"
    android:theme="@style/AppThemeTranAnim">
</activity>

后记、

1.声明:

[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力

2.连接传送门:

更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com

3.联系我

QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328

4.欢迎关注我的微信公众号,最新精彩文章,及时送达:

公众号.jpg

posted on 2018-10-06 22:38  张风捷特烈  阅读(141)  评论(0编辑  收藏  举报