Android slide switching Activity

Source: Internet
Author: User

Screen switching refers to switching on the screen of the same activity.

Slip events are implemented using gesturedetector and ongesturelistener. Viewflipper is inherited to framelayout, so it is a layout where multiple views can be placed. In this example, only one button is put on the first page. When the screen goes down, only one textview is put on each page.

Page:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <viewflipper Android: id = "@ + ID/viewflipper01" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> </viewflipper> </linearlayout> activity code: View plaincopy to clipboardprint? Package COM. yfz; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. gesturedetector; import android. view. motionevent; import android. view. view; import android. view. gesturedetector. ongesturelistener; import android. view. viewgroup. layoutparams; import android. view. animation. animationutils; import android. widget. button; import android. widget. expandablel Istview; import android. widget. textview; import android. widget. viewflipper; public class flip extends activity implements ongesturelistener {private gesturedetector detector; private viewflipper Flipper;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Flipper = (viewfli Pper) This. findviewbyid (R. id. viewflipper01); Flipper. addview (addbuttonbytext ("button"), new layoutparams (layoutparams. fill_parent, layoutparams. wrap_content); detector = new gesturedetector (this);} public view addbuttonbytext (string text) {button BTN = new button (this); BTN. settext (text); Return BTN;} public view addtextbytext (string text) {textview TV = new textview (this); TV. settext (text); TV. setg Ravity (1); Return TV ;}@ override public Boolean ontouchevent (motionevent event) {log. I ("Fling", "activity ontouchevent! "); Return this. detector. ontouchevent (event) ;}@ override public Boolean ondown (motionevent e) {// todo auto-generated method stub return false ;} /*** listen to slide */@ override public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {// todo auto-generated method stub log. I ("Fling", "fling happened! "); If (e1.getx ()-e2.getx ()> 120) {This. flipper. setinanimation (animationutils. loadanimation (this, R. anim. push_left_in); this. flipper. setoutanimation (animationutils. loadanimation (this, R. anim. push_left_out); this. flipper. addview (addtextbytext ("text box"), new layoutparams (layoutparams. fill_parent, layoutparams. wrap_content); this. flipper. shownext (); Return true;} else if (e1.getx ()-e2.getx () <-120) {This. flipper. setinanimation (animationutils. loadanimation (this, R. anim. push_right_in); this. flipper. setoutanimation (animationutils. loadanimation (this, R. anim. push_right_out); this. flipper. showprevious (); Return true;} return true;} @ override public void onlongpress (motionevent e) {// todo auto-generated method stub} @ override public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey) {// todo auto-generated method stub return false;} @ override public void onshowpress (motionevent E) {// todo auto-generated method stub} @ override public Boolean onsingletapup (motionevent e) {// todo auto-generated method stub return false ;}}

Here, the function is to move from right to left to the previous view, move from left to right to the next view, and use different In and Out animations to make the switching effect look more unified.

Sliding Screen implementation:

The android. View. gesturedetector class can detect various gesture events. This class has two callback interfaces to notify specific events:

Gesturedetector. ondoubletaplistener: Used to notify doubletap events. Similar to double-click events, this interface has the following three callback functions:

1. ondoubletap (motionevent E): Notification doubletap gesture,
2. ondoubletapevent (motionevent E): notifies the events in the doubletap gesture, including the Down, up, and move events (this refers to the events that occur between double-click, for example, double-click in the same place will generate a doubletap gesture, while a down and up event will occur in the doubletap gesture, which are notified by the function );
3. onsingletapconfirmed (motionevent E): used to determine whether the click is singletap rather than doubletap. If you click it twice consecutively, It is a doubletap gesture. If you click it only once, after waiting for a while, the system determines that the second click is singletap instead of doubletap, and then triggers the singletapconfirmed event.
Gesturedetector. ongesturelistener: Used to notify normal gesture events. This interface has the following six callback functions:
1. ondown (motionevent E): Down event;
2. onsingletapup (motionevent E): one-click up event;
3. onshowpress (motionevent E): When the down event occurs and the move or up event is not triggered before the event is executed;
4. onlongpress (motionevent E): Long press event;
5. onfling (motionevent E1, motionevent E2, float velocityx, float velocityy): Sliding gesture event;
6. onscroll (motionevent E1, motionevent E2, float distancex, float distancey): drag the event on the screen.
This time, only the onfling method in the ongesturelistener class is used. You can perform other gesture events on your own.
Simple Description:
To implement screen switching, you must first define a gesturedetector:
Private gesturedetector mgesturedetector;

And initialize it in the oncreate function:
Mgesturedetector = new gesturedetector (this );

At the same time, activity must inherit the ongesturelistener interface and implement the onfling method.
In addition, the ontouchevent event of the activity must also be implemented !!
In addition, in this example, there is an animation effect when switching the view. Use the animation class to implement related functions:
Setinanimation: sets the animation used when the view enters the screen. This function has two versions:
A single parameter is accepted, and the type is Android. View. animation. animation;
One parameter can be set to context and INT, which are the context object and the resourceid of the animation.
Setoutanimation: Specifies the animation used when the view exits the screen. The setinanimation parameter function is the same.
Shownext: Call this function to display the next view in framelayout.
Showprevious: Call this function to display the previous view in framelayout.
Animation source file: (In the res directory, create an anim folder and put the following files here)

Push_left_in.xml

View plaincopy to clipboardprint?
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<Translate Android: fromxdelta = "100% P" Android: toxdelta = "0"
Android: Duration = "500"/>
<Alpha Android: fromalpha = "0.1" Android: toalpha = "1.0"
Android: Duration = "500"/>
</Set>

Push_left_out.xml:

View plaincopy to clipboardprint?
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<Translate Android: fromxdelta = "0" Android: toxdelta = "-100% P"
Android: Duration = "500"/>
<Alpha Android: fromalpha = "1.0" Android: toalpha = "0.1"
Android: Duration = "500"/>
</Set>

Push_right_in.xml:

View plaincopy to clipboardprint?
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<Translate Android: fromxdelta = "-100% P" Android: toxdelta = "0"
Android: Duration = "500"/>
<Alpha Android: fromalpha = "0.1" Android: toalpha = "1.0"
Android: Duration = "500"/>
</Set>

Push_right_out.xml:

View plaincopy to clipboardprint?
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<Translate Android: fromxdelta = "0" Android: toxdelta = "100% P"
Android: Duration = "500"/>
<Alpha Android: fromalpha = "1.0" Android: toalpha = "0.1"
Android: Duration = "500"/>
</Set>

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.