Activity Switching Effect

Source: Internet
Author: User

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
  1. Android: fromXDelta = "-100%"
  2. Android: toXDelta = "0%"
  3. Android: duration = "300"/>



  4. Out_to_left.xml (from location 2 to location 1)


    [Html]View plaincopy
    1. Android: duration = "500"/>



    2. In_from_right.xml (from location 3 to location 2)

      [Html]View plaincopy
      1. Android: duration = "500"/>


      2. Note: android: fromXDelta animation start position, android: toXDelta animation end position, android: duration animation time.

        The Android code is as follows:

        [Java]View plaincopy
        1. Public class LeftRightSlideActivity extends Activity {
        2. @ Override
        3. Public void onCreate (Bundle savedInstanceState ){
        4. Super. onCreate (savedInstanceState );
        5. SetContentView (R. layout. main );
        6. Button button = (Button) findViewById (R. id. button1 );
        7. Button. setOnClickListener (new View. OnClickListener (){
        8. @ Override
        9. Public void onClick (View v ){
        10. Intent intent = new Intent ();
        11. Intent. setClass (LeftRightSlideActivity. this, SlideSecondActivity. class );
        12. StartActivity (intent );
        13. // Set the animation to switch from the right to the left.
        14. OverridePendingTransition (R. anim. in_from_right, R. anim. out_to_left );
        15. }
        16. });
        17. }
        18. }

          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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.