1.主MainAc
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; public class TransitionActivity extends AppCompatActivity { private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_transition); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public void explode(View view){ intent=new Intent(this,TransitionActivityTo.class); intent.putExtra("flag",0); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle()); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public void slide(View view){ intent=new Intent(this,TransitionActivityTo.class); intent.putExtra("flag",1); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle()); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public void fade(View view){ intent=new Intent(this,TransitionActivityTo.class); intent.putExtra("flag",2); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle()); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public void share(View view){ View fab=findViewById(R.id.fab_button); intent=new Intent(this,TransitionActivityTo.class); intent.putExtra("flag",3); startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this , Pair.create(fab,"fab") , Pair.create(view,"share")).toBundle()); } }
辅MainAc
import android.os.Build; import android.support.annotation.RequiresApi; 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; import java.util.Objects; public class TransitionActivityTo extends AppCompatActivity { @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); int flag = Objects.requireNonNull(getIntent().getExtras()).getInt("flag"); // 设置不同的动画效果 switch (flag) { case 0: getWindow().setEnterTransition(new Explode()); getWindow().setExitTransition(new Explode()); break; case 1: getWindow().setEnterTransition(new Slide()); getWindow().setExitTransition(new Slide()); break; case 2: getWindow().setEnterTransition(new Fade()); getWindow().setExitTransition(new Fade()); break; case 3: break; } setContentView(R.layout.activity_transition_to); } }
主MainAc.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="100dp" android:onClick="explode" android:text="explode" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:onClick="slide" android:text="slide" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:onClick="fade" android:text="fade" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:transitionName="share" android:onClick="share" android:text="share" tools:targetApi="lollipop" /> <Button android:id="@+id/fab_button" android:layout_width="56dp" android:transitionName="fab" android:layout_height="56dp" android:background="@drawable/ripple_round" android:elevation="5dp" tools:targetApi="lollipop" /> </LinearLayout>
副MainAc.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin"> <View android:id="@+id/holder_view" android:layout_width="match_parent" android:layout_height="300dp" android:transitionName="share" android:background="@color/colorPrimary" tools:targetApi="lollipop" /> <Button android:id="@+id/fab_button" android:transitionName="fab" android:layout_width="56dp" android:layout_height="56dp" android:layout_marginRight="@dimen/activity_horizontal_margin" android:background="@drawable/ripple_round" android:elevation="5dp" android:layout_below="@+id/holder_view" android:layout_marginTop="-26dp" android:layout_alignParentRight="true" tools:targetApi="lollipop" android:layout_alignParentEnd="true" android:layout_marginEnd="@dimen/activity_horizontal_margin" /> <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_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout> </RelativeLayout>Tips:(补学)https://www.jianshu.com/p/692284dc3646 Transition类源码分析