Android Activity screen switching animation-slide switch between left and right, androidactivity
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 ). 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)
Out_to_left.xml (from location 2 to location 1)
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromXDelta="0%p" android:toXDelta="-100%p" android:duration="500" /></set>
In_from_right.xml (from location 3 to location 2)
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromXDelta="100%p" android:toXDelta="0%p" android:duration="500" /></set>
Android: fromXDelta animation start position, android: toXDelta animation end position, android: duration animation time.
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); // you can set the animation to switch from the right, and exit overridePendingTransition (R. anim. in_from_right, R. anim. out_to_left );}});}}
OK. This is the current animation.
In android, how does one implement a sliding activity? How does one slide left and right to implement activity switching?
Do you want to slide the activity or the page? If it is an activity, you can add a gesture to the current page, implement activity switching during sliding, and then add a switchover animation to the activity. If it is a qq effect, you can use a tab to do it, the implementation is very good, or activitygroup, you can also use fragment to do, these implementations are good. For page slide switching, you can use viewpager. This effect is very good. If the code is strong, you can use viewgroup scrolling to achieve the effect.
In Android, how does one implement sliding between the left and right to switch the Activity? each Activity has images, buttons, and so on.
The following two methods are provided:
1. viewPage + fragment/activity implementation. If you are not familiar with fragment, you can use activity to implement it. If you are not familiar with the specific control, the api documentation provides detailed instructions. However, we recommend that you still use fragment, after all, new things are easy to use.
2. Implement gesture judgment in fragment/activity, switch activity, and set animation to achieve visual sliding effect.