“This is the sixth day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

The first: (Activity using overridePendingTransition method is jump animation)

The code in the Activity is as follows

/ * * * click on the button to realize logic * / for the jump in setOnClickListener (new View. An OnClickListener () {@ Override public void onClick (View v) {/ * * * immediately after call startActivity method call overridePendingTransition method * / Intent Intent = new Intent (MainActivity. This, SecondActivity.class); startActivity(intent); overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_left); }});Copy the code

The code under the anim file is as follows

<? The XML version = "1.0" encoding = "utf-8"? > <set xmlns:Android="http://schemas.Android.com/apk/res/Android" Android:shareInterpolator="false" Android:zAdjustment="top"> <translate Android:duration="200" Android:fromXDelta="-100.0%p" Android:toXDelta="0.0" /> </set>Copy the code

Second: (use style to define the Activity toggle animation)

Start with the manifest file

<! < Application Android:allowBackup="true" Android:icon="@mipmap/ ic_Launcher" Android:label="@string/app_name" Android:supportsRtl="true" Android:theme="@style/AppTheme">Copy the code

Enter the AppTheme

<! -- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <! -- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="Android:windowAnimationStyle">@style/activityAnim</item> </style> <! <style name="activityAnim"> <item name="Android:activityOpenEnterAnimation">@anim/slide_in_top</item> <item Name = "Android: activityOpenExitAnimation" > @ anim/slide_in_top < item > < / style >!Copy the code

There are four kinds of animation in windowAnimationStyle

ActivityOpenEnterAnimation / / used to set up open new Activity and into the new show animation activityOpenExitAnimation / / Activity Used to set up open the Activity before the new Activity and destroy an animated show activityCloseEnterAnimation / / is used to set off the current Activity into an Activity on the animation of the show ActivityCloseExitAnimation / / is used to set off the current Activity show animationCopy the code

The test code in the Activity is as follows

/ * * * click on the button, the realization of the Activity of the jump * operation by defining the style the way of the Activity of a jump animation. * / button2 setOnClickListener (new View. An OnClickListener () { @override public void onClick(View v) {/** * Intent = new Intent(mainActivity.this, SecondActivity.class); startActivity(intent); }});Copy the code

3: (use ActivityOptions toggle animation to achieve the Activity jump animation)

The first step

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GetWindow ().requestFeature(window.feature_content_Transitions); Transition explode = TransitionInflater.from(this).inflateTransition(Android.R.transition.explode); getWindow().setEnterTransition(explode); setContentView(R.layout.activity_main); }Copy the code

The second step

/ * * * click on the button, the realization of the Activity of the jump * operation through Android5.0 and above code implementation of the Activity of a jump animation. * / button3 setOnClickListener (new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ThreeActivity.class); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle()); }});Copy the code

Fourth: (use the built-in animation effects after ActivityOptions via style)

Create an activity_explode resource file in the Application project res directory and create a transition directory

<explode xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:duration="300" />
Copy the code

Defining a style file

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <! -- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="Android:windowEnterTransition">@transition/activity_explode</item> <item name="Android:windowExitTransition">@transition/activity_explode</item> </style>Copy the code

Execute jump logic

/ * * * click on the button, the realization of the Activity of the jump operation * implementation Activity by means of Android5.0 and above style jump animation. * / button4 setOnClickListener (new View.OnClickListener() { @Override public void onClick(View v) { /** * Call ActivityOptions. Excessive makeSceneTransitionAnimation achieve animation * / Intent Intent = new Intent (MainActivity. This, FourActivity.class); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle()); }});Copy the code

Fifth: (use ActivityOptions animation sharing component to achieve jump Activity animation)

Define shared components in the layout file in Acitivity_A

<Button Android:id="@+id/button5" Android:layout_width="match_parent" Android:layout_height="wrap_content" Android:layout_below="@+id/button4" Android:layout_marginTop="10dp" Android:layout_marginRight="10dp" Android:layout_marginLeft="10dp" Android:text=" 下 载 "Android:background="@color/colorPrimary" Android:layout_marginLeft="10dp" Android:transitionName="shareNames" />Copy the code

Procedure Associate shared components with the layout file of Acitivity_B

<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" Android:id="@+id/activity_second" Android:layout_width="match_parent" Android:layout_height="match_parent" Android:gravity="center_horizontal" Android:orientation="vertical" Android:transitionName="shareNames" > <TextView Android:layout_width="match_parent" Android:layout_height="match_parent" Android:background="@color/colorAccent" Android:layout_marginTop="10dp" Android:layout_marginBottom="10dp" />Copy the code

Execute jump logic

/ * * * click on the button, the realization of the Activity of the jump * operation through Android5.0 and above Shared component implementation Activity of a jump animation * / button5 setOnClickListener (new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, FiveActivity.class); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, button5, "shareNames").toBundle()); }});Copy the code

Conclusion:

  • OverridePendingTransition method starting from the Android2.0, basically can cover our demand activity jump animation;

  • The ActivityOptions API starts on Android5.0 and allows you to create some cool animations that are more MD style.

  • ActivityOptions also enables excessive animation between two Activity components;