首先来说说以前我在项目中用到的Activity的跳转的效果是怎么实现的:
第一种方法:既然要实现动画效果,那肯定得写个动画啦
1.res文件夹下的anim的文件里创建一个文件,里面写4个动画文件
都是如下的简单的位移动画:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="300" android:fromXDelta="100.0%p" android:toXDelta="0.0" /> </set>
那么,如果要是很多的activity都需要这个动画效果,那么就可以抽出来做一个style了
<style name="MyActivityAnim"> <item name="android:activityOpenEnterAnimation">@anim/slide_right_in</item> <item name="android:activityOpenExitAnimation">@anim/slide_left_out</item> <item name="android:activityCloseEnterAnimation">@anim/slide_left_in</item> <item name="android:activityCloseExitAnimation">@anim/slide_right_out</item> </style>
然后再Application的theme上引用这个style:
<style name="AppTheme" parent="android:Theme.Material.Light"> <!-- --> <item name="android:colorPrimary">#BEBEBE</item> <item name="android:colorPrimaryDark">#FF5AEbFF</item> <item name="android:navigationBarColor">#FFFF4130</item> <item name="android:windowAnimationStyle">@style/MyActivityAnim </item> <item name="android:windowIsTranslucent">true</item> </style>
然后再application的theme里
android:theme="@style/AppTheme"
就ok啦!
如果还想加上其他的动画,也是可以滴!
下面讲一讲Android5.x提供的切换方式。看看会不会更加的流畅和丰富:
1:进入的动画,退出的动画,他们有这三种效果
explode - 从屏幕中间进或出,移动视图;slide - 从屏幕边缘进或者出; fade淡出-改变屏幕的alpha值,添加或者移除视图
如果要在页面A跳到B,则只要在startActivity的时候
MainActivity.this.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());同时,在BActivity中设置进入的动画和退出的动画
代码如下:
A:
package com.activityanimation; import android.annotation.TargetApi; import android.app.ActivityOptions; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button explode = (Button) findViewById(R.id.explode); Button fade = (Button) findViewById(R.id.fade); Button slide = (Button) findViewById(R.id.slide); explode.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,TwoActivity.class); intent.putExtra("mode",1); MainActivity.this.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle()); } }); fade.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,TwoActivity.class); intent.putExtra("mode",2); MainActivity.this.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle()); } }); slide.setOnClickListener(new View.OnClickListener() { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,TwoActivity.class); intent.putExtra("mode",3); MainActivity.this.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle()); } }); } }B:
package com.activityanimation; import android.annotation.TargetApi; import android.app.ActivityOptions; import android.content.Intent; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.transition.Explode; import android.transition.Fade; import android.transition.Slide; import android.view.View; import android.view.Window; import android.widget.Button; public class TwoActivity extends BaseActivity { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int mode = getIntent().getIntExtra("mode",1); switch (mode){ case 1: getWindow().setEnterTransition(new Explode()); break; case 2: getWindow().setEnterTransition(new Fade()); break; case 3: getWindow().setEnterTransition(new Slide()); break; } setContentView(R.layout.activity_two); Button btn_exit = (Button) findViewById(R.id.btn_exit); btn_exit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(TwoActivity.this, MainActivity.class); TwoActivity.this.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(TwoActivity.this).toBundle()); } }); } }接下来就是漂亮的运行效果咯: