Activity Switching Effect
Previously, redirection from AActivity to BActivity was implemented as follows:
Intent intent = new Intent (AActivity. this, BActivity. class );
StartActivity (intent );
Finish ();
Today we want to achieve the animation effect. I feel that it is too hard to achieve this. I searched the internet and used the following implementation method:
During Android development, you may often encounter switching between activities. The following describes how to achieve switching between left and right sliding. First, let's take a look at the implementation of Activity switching, A method has been added in Activity since Android2.0:
Public void overridePendingTransition (int enterAnim, int exitAnim)
Where:
EnterAnim defines the animation when the Activity enters the screen
ExitAnim defines the animation when the Activity exits the screen
The overridePendingTransition method must be followed by the startActivity () or finish () method.
Android has several built-in animation effects. For details, see android. R. anim. In general, we need to define the screen switching effect. First, let's first understand the location definition of the Activity, such:
It can be seen that there is no X axis at the bottom of the screen of the mobile phone, and the Y axis on the left of the screen. When the Activity value on the X axis is-100% p, It is right on the left of the screen (Position 1 ), when the X axis value is 0% p, it is right in the screen (Position 2), and when X = 100% p, It is right on the right side of the screen (position 3 ).
From one to two:
From 2 to 1:
From 3 to 2:
After knowing the position, we can achieve switching between the left and right slides. First, let the Activity to exit move from position 2 to position 1, and let the Activity to be exited move from position 3 to position 2, in this way, the switching from left to right can be achieved.
The implementation process is as follows: first define two animations, create the anim directory in the res directory, and then create the xml file of the animation in the directory: out_to_left.xml (exit the animation from the left), in_from_right.xml (entering the animation from the right)
In_from_right.xml (from location 1 to location 2)
[Html]View plaincopy
-
-
- Android: fromXDelta = "-100%"
- Android: toXDelta = "0%"
- Android: duration = "300"/>
-
Out_to_left.xml (from location 2 to location 1)
[Html]View plaincopy
-
-
- Android: duration = "500"/>
-
In_from_right.xml (from location 3 to location 2)[Html]View plaincopy
-
-
- Android: duration = "500"/>
-
Note: android: fromXDelta animation start position, android: toXDelta animation end position, android: duration animation time.The Android code is as follows:
[Java]View plaincopy
- Public class LeftRightSlideActivity extends Activity {
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
-
- Button button = (Button) findViewById (R. id. button1 );
- Button. setOnClickListener (new View. OnClickListener (){
- @ Override
- Public void onClick (View v ){
- Intent intent = new Intent ();
- Intent. setClass (LeftRightSlideActivity. this, SlideSecondActivity. class );
- StartActivity (intent );
- // Set the animation to switch from the right to the left.
- OverridePendingTransition (R. anim. in_from_right, R. anim. out_to_left );
- }
- });
- }
- }
As follows:
Although the implementation of left and right sliding switching is very simple, it is very important to understand the principle. Mastering the principle can give full play to the imagination to design a variety of animation effects, hoping to help beginners.
In the future, we will gradually sort out the switching animation effects used in some projects.