Android 5.0 Activity switching Animation
In Androiod5.0, Google defines the Material Design specifications. animation switching gives users an intuitive and consistent experience, which is also highly praised by Google. to this end, Android supports a lot of cool animation effects. here is an official introduction to Material Design Animation for Android. the following describes the switching effect of one Activity (for example ). when the last two activities share a common UI element, this animation effect can be used to provide a consistent user experience. step 1. create an Android Application Project, which contains two activities; 2. the following describes the MainActivity code and XML layout. The code and layout are very simple. explain the code of the onClick () method. the second parameter of the makeSceneTransactionAnimation () method is used to tell the specified common UI element. The parameter view is used here. the third parameter of the makeSceneTransactionAnimation () method is "robot", which corresponds to the android: transactionName = "robot" value in the XML layout.
package com.example.garena.myapplication;import android.app.ActionBar;import android.app.Activity;import android.app.ActivityOptions;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final View shareView = findViewById(R.id.share_element_image_view); shareView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); ActivityOptions options = ActivityOptions .makeSceneTransitionAnimation(MainActivity.this, shareView, "robot"); startActivity(intent, options.toBundle()); } }); ActionBar actionBar = getActionBar(); if (actionBar != null) { actionBar.setTitle(R.string.first_activity); } }}
<LinearLayout 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:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <ImageView android:id="@+id/share_element_image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:transitionName="robot" /></LinearLayout>
3. the following is the code and layout of SecondActivity. In The onClick () method, call finishAfterTransaction () to finish the activity. in the XML layout, android: transactionName = "roboto" is also required to identify common UI elements.
package com.example.garena.myapplication;import android.app.ActionBar;import android.app.Activity;import android.os.Bundle;import android.view.View;public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); ActionBar actionBar = getActionBar(); if (actionBar != null) { actionBar.setTitle(R.string.second_activity); } View btnBack = findViewById(R.id.btn_back); btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finishAfterTransition(); } }); }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/title_icon_image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:transitionName="robot" /> <Button android:id="@+id/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/back" /></LinearLayout>
4. running Effect (for example). You can see from the final running result that when you click the small robot icon to enter SecondActivity, you can see that the small robot icon moves to the position of the small robot icon SecondActivity. click the Back button of SecondActivity to move the robot icon to the position of the robot icon in MainActivity.