Mobile Development Guide: Introduction to Android Transition Framework

Mobile Development Guide: Introduction to Android Transition Framework

【51CTO translation】The Android Transition framework allows us to configure various appearance changes in the application user interface. You can implement animated transitions within the application screen, define each stage as a scene, and control how the application transitions from one display scene to another.

In today's article, we will build a simple application and create a set of animated transition effects for it. To complete this task, you need to prepare layout and drawable files in XML, and then use Java to configure and apply this transition mechanism. We will define two scenes, in which the same set of view items will be arranged in different ways on the device screen. When you use the Transition framework, Android will automatically complete the animated transition effects during the transition between the two scenes.

1. Create an application

***step

As the first step of the tutorial, we will first create a new application in the IDE of our choice. You will need to use at least SDK 19 for these Transition classes to work properly, so if you plan to support earlier versions, we will need to perform some additional steps.

First, specify a main Activity and layout file for the application, and choose start_layout.xml as the name for the layout. We will then add other layout files and use the Transition mechanism to switch between different display layouts. The following screenshots show the specific implementation process of this process in Android Studio.

Step 2

Next we prepare some drawables to use in the Transition. We will prepare four circular patterns, each filled with a different gradient color. In the drawable resource directory of this sample application, first create a new file called shape1.xml. Add the shape with the following code:

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < shape   xmlns:android = "http://schemas.android.com/apk/res/android"   
  3. android:dither = "true"   
  4. android:shape = "oval"   >   
  5.    
  6. < gradient   
  7. android:endColor = "#66ff0000"   
  8. android:gradientRadius = "150"   
  9. android:startColor = "#ffffcc00"   
  10. android:type = "radial"   
  11. android:useLevel = "false"   />   
  12.    
  13. < size   
  14. android:height = "100dp"   
  15. android:width = "100dp"   />   
  16.    
  17. </ shape >   

The above code constructs a circular pattern filled with gradient colors. The four graphics are exactly the same in size and style, and only differ in color. Of course, you may need to prepare different versions of graphics for devices with different pixel densities. Use the following code to create shape2.xml:

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>  
  2. < shape   xmlns:android = "http://schemas.android.com/apk/res/android"  
  3. android:dither = "true"  
  4. android:shape = "oval"   >  
  5.   
  6. < gradient  
  7. android:endColor = "#66ffcc00"  
  8. android:gradientRadius = "150"  
  9. android:startColor = "#ff00ff00"  
  10. android:type = "radial"  
  11. android:useLevel = "false"   />  
  12.   
  13. < size  
  14. android:height = "100dp"  
  15. android:width = "100dp"   />  
  16.   
  17. </ shape >  

Now add shape3.xml:

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>  
  2. < shape   xmlns:android = "http://schemas.android.com/apk/res/android"  
  3. android:dither = "true"  
  4. android:shape = "oval"   >  
  5.   
  6. < gradient  
  7. android:endColor = "#6600ff00"  
  8. android:gradientRadius = "150"  
  9. android:startColor = "#ff0000ff"  
  10. android:type = "radial"  
  11. android:useLevel = "false"   />  
  12.   
  13. < size  
  14. android:height = "100dp"  
  15. android:width = "100dp"   />  
  16.   
  17. </ shape >  

***Add shape4.xml:

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>  
  2. < shape   xmlns:android = "http://schemas.android.com/apk/res/android"  
  3. android:dither = "true"  
  4. android:shape = "oval"   >  
  5.   
  6. < gradient  
  7. android:endColor = "#660000ff"  
  8. android:gradientRadius = "150"  
  9. android:startColor = "#ffff0000"  
  10. android:type = "radial"  
  11. android:useLevel = "false"   />  
  12.   
  13. < size  
  14. android:height = "100dp"  
  15. android:width = "100dp"   />  
  16.   
  17. </ shape >  

We will use these graphics as ImageButtons in two layout scenes.

#p#

2. Create a layout scene

***step

Next, we need to define two types of scenarios that will transition between several XML layouts. First, we deal with the main layout file that you have already added when you created the application, that is, start_layout.xml. Open it and switch to the XML editing tab. Use the following code to use RelativeLayout:

  1. < RelativeLayout   xmlns:android = "http://schemas.android.com/apk/res/android"  
  2. xmlns:tools = "http://schemas.android.com/tools"  
  3. android:layout_width = "match_parent"  
  4. android:layout_height = "match_parent"  
  5. android:background = "#ff000000"  
  6. android:id = "@+id/base"  
  7. tools:context = ".TransitionsActivity" >  
  8.   
  9. </RelativeLayout>  

We have added a background color and an ID to the layout. The purpose of this ID is to ensure that Android handles the transition between the different scenes. You will use the same ID again in the second scene. When you transition between the two scenes, Android will animate the views with the same ID in each scene. If they do not have the same ID, then Android will treat them as completely different items and simply handle the transition by fading in or out.

In RelativeLayout, create an ImageButton for each of the shapes we created earlier:

  1. < ImageButton  
  2. android:layout_width = "wrap_content"  
  3. android:layout_height = "wrap_content"  
  4. android:id = "@+id/btn1"  
  5. android:src = "@drawable/shape1"  
  6. android:background = "#00000000"  
  7. android:contentDescription = "shape"  
  8. android:layout_alignParentLeft = "true"  
  9. android:layout_alignParentTop = "true"  
  10. android:onClick = "changeScene" />  
  11.   
  12. < ImageButton  
  13. android:layout_width = "wrap_content"  
  14. android:layout_height = "wrap_content"  
  15. android:id = "@+id/btn2"  
  16. android:src = "@drawable/shape2"  
  17. android:background = "#00000000"  
  18. android:contentDescription = "shape"  
  19. android:layout_alignParentRight = "true"  
  20. android:layout_alignParentTop = "true"  
  21. android:onClick = "changeScene" />  
  22.   
  23. < ImageButton  
  24. android:layout_width = "wrap_content"  
  25. android:layout_height = "wrap_content"  
  26. android:id = "@+id/btn3"  
  27. android:src = "@drawable/shape3"  
  28. android:background = "#00000000"  
  29. android:contentDescription = "shape"  
  30. android:layout_alignParentLeft = "true"  
  31. android:layout_alignParentBottom = "true"  
  32. android:onClick = "changeScene" />  
  33.   
  34. < ImageButton  
  35. android:layout_width = "wrap_content"  
  36. android:layout_height = "wrap_content"  
  37. android:id = "@+id/btn4"  
  38. android:src = "@drawable/shape4"  
  39. android:background = "#00000000"  
  40. android:contentDescription = "shape"  
  41. android:layout_alignParentRight = "true"  
  42. android:layout_alignParentBottom = "true"  
  43. android:onClick = "changeScene" />  

Note that each graphic button has its own ID - as does the second layout we created - plus an onClick attribute. We will then add this method to the main Activity and start the transition when the user clicks on any of the graphics.

Now we will see a preview of the entire layout in the IDE, but in some cases you will need to actually run the app on a device or simulator to see the gradient and/or transparency effects. These graphics are arranged in the four corners of the screen, as shown in the figure below.

Step 2

The first layout we created will be displayed as the starting state of the transition process. Now let's create a second layout file for the scene and use it as the ending state of the transition process. Add a new file to our application layout resource directory and name it end_layout.xml. Switch to the text editing tab and enter the following code:

  1. < RelativeLayout   xmlns:android = "http://schemas.android.com/apk/res/android"  
  2. xmlns:tools = "http://schemas.android.com/tools"  
  3. android:layout_width = "match_parent"  
  4. android:layout_height = "match_parent"  
  5. android:background = "#ff000000"  
  6. android:id = "@+id/base"  
  7. tools:context = ".TransitionsActivity" >  
  8.   
  9. < ImageButton  
  10. android:layout_width = "wrap_content"  
  11. android:layout_height = "wrap_content"  
  12. android:id = "@+id/btn1"  
  13. android:src = "@drawable/shape1"  
  14. android:background = "#00000000"  
  15. android:contentDescription = "shape"  
  16. android:layout_alignParentRight = "true"  
  17. android:layout_alignParentBottom = "true"  
  18. android:onClick = "changeScene" />  
  19.   
  20. < ImageButton  
  21. android:layout_width = "wrap_content"  
  22. android:layout_height = "wrap_content"  
  23. android:id = "@+id/btn2"  
  24. android:src = "@drawable/shape2"  
  25. android:background = "#00000000"  
  26. android:contentDescription = "shape"  
  27. android:layout_alignParentLeft = "true"  
  28. android:layout_alignParentBottom = "true"  
  29. android:onClick = "changeScene" />  
  30.   
  31. < ImageButton  
  32. android:layout_width = "wrap_content"  
  33. android:layout_height = "wrap_content"  
  34. android:id = "@+id/btn3"  
  35. android:src = "@drawable/shape3"  
  36. android:background = "#00000000"  
  37. android:contentDescription = "shape"  
  38. android:layout_alignParentRight = "true"  
  39. android:layout_alignParentTop = "true"  
  40. android:onClick = "changeScene" />  
  41.   
  42. < ImageButton  
  43. android:layout_width = "wrap_content"  
  44. android:layout_height = "wrap_content"  
  45. android:id = "@+id/btn4"  
  46. android:src = "@drawable/shape4"  
  47. android:background = "#00000000"  
  48. android:contentDescription = "shape"  
  49. android:layout_alignParentLeft = "true"  
  50. android:layout_alignParentTop = "true"  
  51. android:onClick = "changeScene" />  
  52.   
  53. </RelativeLayout>  

Now let's take a moment to review the above layout code. It is exactly the same as the first layout except for the position of the various graphic buttons. Each graphic has been moved from its starting position to its diagonal position. The transition process will therefore swap the positions of the graphics, that is, guide them to the diagonal position on the screen.

#p#

3. Transition between different scenes

***step

Now that we have defined two layouts, we need to use the transition mechanism to complete the movement process between them. Open the main Activity class in the application. You will need to use the following import statements:

  1. import android.transition.AutoTransition;
  2. import android.transition.Scene;
  3. import android.transition.Transition;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.view.animation.AccelerateDecelerateInterpolator;
  7. import android.widget.RelativeLayout;
  8. import android.transition.TransitionManager;

In the Activity class declaration, before the onCreate method, we need to add the following instance variables to apply the transition mechanism:

  1. //scenes to transition  
  2. private Scene scene1, scene2;
  3. //transition to move between scenes  
  4. private Transition transition;
  5. //flag to swap between scenes  
  6. private   boolean start;

Step 2

Now let's prepare for the transition, which will begin when the user clicks on any shape. In onCreate, we'll add the following after the existing code that the IDE has already entered:

  1. //get the layout ID  
  2. RelativeLayout baseLayout = (RelativeLayout)findViewById(R.id.base);
  3.   
  4. //first scene  
  5. ViewGroup startViews = (ViewGroup)getLayoutInflater()
  6. .inflate(R.layout.start_layout, baseLayout, false );
  7.   
  8. //second scene  
  9. ViewGroup endViews = (ViewGroup)getLayoutInflater()
  10. .inflate(R.layout.end_layout, baseLayout, false );

We first need to define the base scene, which is the ID we set for the layout contained in the two scene layout files. Next, we also need to define the two scenes that serve as the start and end states of the transition process, specifying the layout file name and the base scene contained in it. In this way, Android will be able to transition between the two scenes as needed and treat any view elements with the same ID in different scenes as the same object, so that the scene switching can show an animated change effect.

Next, we define two scenes as the start and end states of the transition process, still in onCreate:

  1. //create the two scenes  
  2. scene1 = new Scene(baseLayout, startViews);
  3. scene2 = new Scene(baseLayout, endViews);

We need to pass the base layout and the relevant scene layout to each constructor. Now we can reference these scenes when defining the transition process.

Step 3

Next we prepare to perform the transition, still in onCreate:

  1. //create transition, set properties  
  2. transition = new AutoTransition();
  3. transition.setDuration( 5000 );
  4. transition.setInterpolator( new AccelerateDecelerateInterpolator());
  5.   
  6. //initialize flag  
  7. start = true ;

Android provides a range of transition types to choose from, and you can use different animation effects according to the scene change method you need. In today's example, we chose AutoTransition, so Android will calculate how to achieve the transition based on the properties of the two changing scenes. Interested friends can also click here to view more options related to Transition references.

We set the duration and interpolation for the transition. You can also set a start delay for the entire change mechanism if you want. Finally, we set the boolean flag to true by initialization. For simplicity, we will use the method of clicking on any graphic to activate the scene transition, but this is just to demonstrate the actual functionality of the example.

Step 4

You must remember that we have added the onClick attribute to the graphic button when creating the layout XML file. Now we need to add this method to the Activity:

  1. public   void changeScene(View v){
  2.   
  3. //check flag  
  4. if (start) {
  5. TransitionManager.go(scene2, transition);
  6. start = false ;
  7. }
  8. else {
  9. TransitionManager.go(scene1, transition);
  10. start = true ;
  11. }
  12. }

We use an Activity to transition from the current scene to another scene, where a Boolean flag keeps track of the type of scene we are currently using. We also specify the Transition object that we created earlier to ensure that the execution of the switch matches the expected effect.

You should now be able to run your application and see the transition happen when you click on any of the shapes. Each time you click, the transition will slowly move the shapes diagonally across the screen, and clicking again will return them to their original positions.

Summary

In today's article, we have actually only begun to understand what kind of design solutions and transition effects we can achieve with the Android Transition framework. To introduce more transition mechanisms in your own application, you can click here to view other methods in the TransitionManager class, including beginDelayedTransition and transitionTo. In addition, you may also want to try to use TransitionSet to combine multiple transition mechanisms, such as using gradients and movement effects from different transition mechanisms at the same time. Depending on the complexity of the transition mechanism, you may also need to use the TransitionValues ​​class, which can provide the ability to reference data values ​​related to the corresponding transition. If you want to learn more about technical means related to scene processing, you can also click here to view the relevant description of the Scene class.

Original link: An Introduction to Android Transitions

Nuka-Cola Translation

<<:  Summary of iOS development resources in early September

>>:  Five programming languages ​​you must master in the next two years_Mobile Technology Semi-monthly Issue 41_51CTO.com

Recommend

How can we prevent cancer before it happens?

Regarding tumors, although early diagnosis and ea...

AI showdown: How does Google view its battle with OpenAI?

[Editor's Note] Google, as a leader in the fi...

Using deep neural networks to solve the problem of NER named entity recognition

This article is organized as follows: What is Nam...

Build a user growth system from scratch?

The user growth system is an operational means fo...

Apple's new patent: The strap can power the Apple Watch

According to foreign media reports, the US Patent...

GDC 2015: The secrets of Chinese mobile game social operations

Recently, KTplay, as China's largest mobile g...

How to develop an online event planning plan?

Even those who are just starting out in operation...