Android 在5.0(API 级别 21)中提供了三种Transition 类型
进入:一个进入的过渡动画决定Activity中的所有的视图怎么进入屏幕
退出:一个退出的过渡动画决定一个Activity中的所有视图怎么退出屏幕
共享元素:一个共享因素过渡动画决定两个Activities之间的过渡,怎么共享它们的视图
进入和退出的效果包括:
1.explode(分解)-----从屏幕中间进入或退出,移动视图
2.slide(滑动)---------从屏幕边缘进入或退出,移动视图
3.fade(淡出)---------通过改变屏幕上视图的不透明度达到添加或移除视图
共享元素包括:
1.changeBounds -------改变目标视图的布局边界
2.changeClipBounds ------裁剪目标视图边界
3.changeTransform --------改变目标视图的缩放比例和旋转角度
4.changeImageTransform -----改变目标图片的大小和缩放比例
效果图如下:
如何使用过渡动画:
1.比如从ActivityA跳转到ActivityB
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
2.ActivityB中
getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
3.在ActivityB中写具体的进入的动画效果
getWindow().setEnterTransition(new Explode());
getWindow().setEnterTransition(new Slide());
getWindow().setEnterTransition(new Fade());
4.在ActivityB中设置离开ActivityB的动画
getWindow().setExitTransition(new Explode())
getWindow().setExitTransition(new Slide());
getWindow().setExitrTransition(new Fade());
5.共享元素的设置:
首先给要共享的元素设置属性 AB两个活动中都要设置
android:transitionName="xxx"
启动方式
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this,view,"share").toBundle());
多个元素共享的书写方式
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, Pair.create(view,"share"),Pair.create(fab,"fab")).toBundle());
6.实列代码:
活动A代码以及布局
package com.example.a37443.trasition;
import android.app.ActivityOptions;
import android.content.Intent;
import android.provider.Contacts;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
public class MainActivity extends AppCompatActivity {
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onclick(View view){
switch(view.getId()){
case R.id.btn_1:
intent = new Intent(this,Main2Activity.class);
intent.putExtra("flag",0);
startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
break;
case R.id.btn_2:
intent = new Intent(this,Main2Activity.class);
intent.putExtra("flag",1);
startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
break;
case R.id.btn_3:
Intent intent = new Intent(this,Main2Activity.class);
intent.putExtra("flag",2);
startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
break;
case R.id.fab_button:
View fab = findViewById(R.id.fab_button);
intent = new Intent(this,Main2Activity.class);
intent.putExtra("flag",3);
startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this,view,"share").toBundle());
break;
default:
break;
}
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:onClick="onclick"
android:text="explode"/>
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:onClick="onclick"
android:text="slide" />
<Button
android:id="@+id/btn_3"
android:layout_width="match_parent"
android:layout_height="100dp"
android:onClick="onclick"
android:text="fade"/>
<Button android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fab_button"
android:onClick="onclick"
android:transitionName="fab"
android:background="@drawable/icon_person"
android:elevation="5dp"/>
</LinearLayout>
活动B代码以及布局:
package com.example.a37443.trasition;
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.Window;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
int flag = getIntent().getExtras().getInt("flag");
switch (flag){
case 0:
getWindow().setEnterTransition(new Explode());
break;
case 1:
getWindow().setEnterTransition(new Slide());
break;
case 2:
getWindow().setEnterTransition(new Fade());
getWindow().setExitTransition(new Fade());
case 3:
break;
}
setContentView(R.layout.activity_main2);
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/holder_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:transitionName="share"
android:background="@drawable/icon_person">
</View>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:layout_below="@+id/holder_view">
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/button"
android:layout_below="@+id/button4"
android:layout_marginTop="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:id="@+id/button4"
android:layout_alignParentStart="true"/>
</RelativeLayout>
</RelativeLayout>
学习路上且行且记录~