Android Activity screen Toggle Animation (one)-swipe left and right to toggle

Source: Internet
Author: User

I've been working on Android for a while, and have taken the time to take a break and get ready to share some of the projects with Android experience. In the Android development process, often encounter the transition between the effects of the problem, the following describes how to achieve the transition between left and right sliding, first understand the implementation of activity switching, starting from Android2.0 in the activity to add a method: public void overridependingtransition (int enteranim, int exitanim) Where: Enteranim defines the animation when activity enters the screen Exitanim define activity fallback The animation Overridependingtransition method must be behind the startactivity () or the finish () method when the screen is out. Android has several animations built in and can be seen on Android. R.anim class. In general, we need to define the effect of screen switching. First, we first understand the location definition of the activity, such as:

   can be seen from the phone screen under the x-axis, the left side of the screen is the y-axis, when the activity on the x-axis value of -100%p, just on the left side of the screen (position 1), when the x-axis value is 0%p, just in the screen (position 2), when x=100% P is just on the right side of the screen (position 3).   Clear the position, we can achieve left and right sliding transition effect, first let the activity to exit from position 2 to position 1, while allowing the entry activity from position 3 to move position 2, so you can switch from left to right jquery effects. The   implementation process is as follows, first defining 2 animations, creating the Anim directory in the Res directory, and then animating the XML file in the directory: Out_to_left.xml (exiting the animation from the left), In_from_right.xml (from the right into the animation)   Out_to_left.xml (move from position 2 to position 1)  1<?xml version= "1.0" encoding= "Utf-8"? >2<set xmlns:android= "/http Schemas.android.com/apk/res/android "android:interpolator=" <a href= "Http://my.oschina.net/asia" class= "Referer "target=" _blank "> @android </a>: Anim/accelerate_interpolator" >3    <translate android: Fromxdelta= "0%p" Android:toxdelta= " -100%p" 4        android:duration= "/>5</set>in_" From_right.xml (move from position 3 to position 2)  1<?xml version= "1.0" encoding= "Utf-8"? >2<set xmlns:android= "/http Schemas.android.com/apk/res/android "android:interpolator=" <a href= "Http://my.oschina.net/asia" class= "Referer" target= "_blank" > @android </a>: Anim/accelerate_interpolator ">3    <translate Android:fromxdelta= "100%p" Android:toxdelta= "0%p" 4        android:duration= "/>5</set" > Note: Android:fromxdelta the start of the animation position,  android:toxdelta the end of the animation position, android:duration the time of the animation. The Android code is as follows: Http://www.huiyi8.com/webyuanma/ 01public class Leftrightslideactivity extends Activity {02  & nbsp @Override03     public void onCreate (Bundle savedinstancestate) {04        Super.oncreate ( Savedinstancestate);05        Setcontentview (R.layout.main);  06         07        Button button = (button) Findviewbyid (r.id.button1 );08        Button.setonclicklistener (new View.onclicklistener () { 09            @Override10             public void OnClick (View v) {11    &NB Sp           Intent Intent = new Intent ();12                Intent.setclass (Leftrightslideactivity.this, Slidesecondactivity.class);13            & nbsp   StartActivity (Intent);14               //Set toggle animation, enter from right, exit on left 15                overridependingtransition (r.anim.in_from_right, r.anim.out_to_left);               16           }17       );18   }19}:       Although the implementation of the left and right sliding transitions is simple, it is important to understand the principle Master the principle can give full play to the imagination to design a variety of animation effects, hope to some beginners to help.   later in slowly finishing some of the items used in the transition animation effect.

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.