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)

which

Enteranim defining animations when activity enters the screen

Exitanim defining animations when activity exits the screen

The Overridependingtransition method must be behind the startactivity () or the finish () method.

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:

As can be seen in 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 before the screen (position 2), when x=100%p just right on 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 that can be achieved from the left and right switching effect.

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)

?
12345 <?xml version="1.0" encoding="utf-8"?><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">    <translate android:fromXDelta="0%p" android:toXDelta="-100%p"        android:duration="500" /></set>

In_from_right.xml (move from position 3 to position 2)

?
12345 <?xml version="1.0" encoding="utf-8"?><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">    <translate android:fromXDelta="100%p" android:toXDelta="0%p"        android:duration="500" /></set>

Note: The Android:fromxdelta animation starts at the position where the Android:toxdelta animation ends, android:duration the animation's time.

The Android code is as follows:

?
12345678910111213141516171819 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);                //设置切换动画,从右边进入,左边退出                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);                            }        });    }}

As follows:

Although the implementation of the left and right sliding switch is very simple, but the understanding of the principle is very important, 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 organize some projects used in the transition animation effect.

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

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.