目录
- 一:5.0之前的实现效果
- 实现
- 二: 5.0之后如何实现
- 分解、滑动进入、淡入淡出
- 共享元素动画
一:5.0之前的实现效果
1. 实现
(1) 添加动画
anim_enter
<?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="@android:integer/config_mediumAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0%p" />
<!--<alpha-->
<!--android:duration="@android:integer/config_mediumAnimTime"-->
<!--android:fromAlpha="0.0"-->
<!--android:toAlpha="1.0" />-->
</set>
anim_exit
<?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="@android:integer/config_mediumAnimTime"
android:fromXDelta="0%p"
android:toXDelta="-100%p" />
<!--<alpha-->
<!--android:duration="@android:integer/config_mediumAnimTime"-->
<!--android:fromAlpha="1.0"-->
<!--android:toAlpha="0.0" />-->
</set>
back_enter
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="@android:integer/config_mediumAnimTime"
android:fromXScale="1.5"
android:fromYScale="1.5"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:toXScale="1.0"
android:toYScale="1.0" />
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="0.4"
android:toAlpha="1.0" />
</set>
back_exit
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="@android:integer/config_mediumAnimTime"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:toXScale="0.4"
android:toYScale="0.4" />
<alpha android:duration="@android:integer/config_mediumAnimTime" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>
(2) 在下面的styles 中添加样式
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:fitsSystemWindows">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowAnimationStyle">@style/fade</item>
</style>
<!-- activiy 切换动画 -->
<style name="fade" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/anim_enter</item>
<item name="android:activityOpenExitAnimation">@anim/anim_exit</item>
<item name="android:activityCloseEnterAnimation">@anim/back_enter</item>
<item name="android:activityCloseExitAnimation">@anim/back_exit</item>
</style>
(3) 解释说明
android:activityOpenExitAnimation
当我们从 A1 启动 A2 时,A1 从屏幕上消失
android:activityOpenEnterAnimation
当我们从 A1 启动 A2 时,A2 出现在屏幕上
android:activityCloseExitAnimation
当我们从 A2 退出回到 A1 时,A2 从屏幕上消失
android:activityCloseEnterAnimation
当我们从 A2 退出回到 A1 时,A1 出现在屏幕上
二: 5.0之后如何实现
1. 分解、滑动进入、淡入淡出
将原先的跳转改成如下方式
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Act2.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());
} else {
startActivity(intent);
}
}
});
修改第二个Activity
package com.allens.intent2demo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.transition.Explode;
import android.transition.Fade;
import android.transition.Slide;
import android.transition.Transition;
import android.view.Window;
/**
* 描述:
* <p>
* Created by allens on 2018/1/3.
*/
public class Act2 extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// 设置contentFeature,可使用切换动画
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
// init_explode();// 分解
// init_Slide();//滑动进入
init_fade();//淡入淡出
}
setContentView(R.layout.activity_2);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void init_fade() {
Transition transition = new Fade().setDuration(200);
getWindow().setEnterTransition(transition);
getWindow().setExitTransition(transition);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void init_Slide() {
Transition transition = new Slide().setDuration(200);
getWindow().setEnterTransition(transition);
getWindow().setExitTransition(transition);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void init_explode() {
Explode explode = new Explode();
explode.setDuration(200);
getWindow().setEnterTransition(explode);
}
}
注意,一定要在加载布局之前设置,判断一下版本号,5.0之后才有
2. 共享元素动画
修改Activity
package com.allens.intent2demo;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Act2.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Pair<View, String> pair1 = Pair.create(((View) btn), "mybtn");
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, pair1).toBundle());
} else {
startActivity(intent);
}
}
});
}
}
修改第一个activity 中的 xml
需要共享的元素加上android:transitionName
例如
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="共享元素动画"
android:transitionName="mybtn"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
修改第二个activity中的Button 同样加上
android:transitionName="mybtn"
如果需要共享多个控件
使用
startSharedElementAnimation(Window window,
Pair<View, String>... sharedElements)
例如
Pair<View, String> pair1 = Pair.create(((View) btn), "mybtn");
Pair<View, String> pair2 = Pair.create(((View) btn), "mybtn1");
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, pair1,pair2).toBundle());