In Androiod5.0, Google defines the specifications for the material design. And animation switching, can give users an intuitive and consistent experience, is also respected by Google.
For this reason, in Android5.0, Android has supported a number of cool animations. This is an introduction to Android's official animation of material design.
The transition effect of one of the activity (such as) is described below. When the next two activity has a common UI element, it is appropriate to use this animated effect to give the user a consistent experience.
Implementation steps
1. Create a new Android app project with two activity;
2. The following is the mainactivity code and XML layout;
The code and layout are very simple. Explain the code for the onclick () method.
Makescenetransactionanimation () method The second parameter is used to tell the specified common UI element which is, and here is Shareview.
The third parameter of the Makescenetransactionanimation () method is "robot", and the value of android:transactionname= "robot" inside the XML layout corresponds to one by one.
Packagecom.example.garena.myapplication;ImportAndroid.app.ActionBar;Importandroid.app.Activity;Importandroid.app.ActivityOptions;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); FinalView Shareview =Findviewbyid (R.id.share_element_image_view); Shareview.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (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); } }}
<LinearLayoutxmlns: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 "> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" /> <ImageViewAndroid: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 the secondactivity;
Inside the onclick () method, call Finishaftertransaction () to finish activity.
In the XML layout, it is also necessary to use Android:transactionname= "Roboto" to identify common UI elements.
Packagecom.example.garena.myapplication;ImportAndroid.app.ActionBar;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View; Public classSecondactivityextendsActivity {@Overrideprotected voidonCreate (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 (NewView.onclicklistener () {@Override Public voidOnClick (View v) {finishaftertransition (); } }); }}
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:gravity= "Center"android:orientation= "vertical"> <ImageViewAndroid:id= "@+id/title_icon_image_view"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/ic_launcher"Android:transitionname= "Robot" /> <ButtonAndroid:id= "@+id/btn_back"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/back" /></LinearLayout>
4. Operation effect (e.g.);
As you can see from the final run, when you click on the small robot icon to enter Secondactivity, you can see the small robot icon move to the location of the small robot icon Secondactivity.
Click the Secondactivity Back button and the small robot icon will move to the location of the Mainactivity small robot icon.
Android 5.0 Activity Toggle Animation