Simple sliding switching page with Ipone-like switching effect

Source: Internet
Author: User

Java code
Public class Main extends Activity implements OnGestureListener,
OnTouchListener {
// ViewAnimator is not directly used. Instead, ViewFlipper and ViewSwitcher are used. ViewFlipper can be used to specify the switching effect between multiple views in FrameLayout. It can be specified at one time or a separate effect can be specified during each switching. This class provides the following functions:
// IsFlipping: used to determine whether View switching is in progress
// SetFilpInterval: set the time interval for switching between views.
// StartFlipping: Use the interval set above to start switching all views. The switching will be conducted cyclically.
// StopFlipping: Stop View switching
Private ViewFlipper viewFlipper;
Private GestureDetector gestureDetector;
 
Private Button pre1Button;
Private Button next1Button;
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
 
Private void init (){
Pre1Button = (Button) findViewById (R. id. preButton1 );
Next1Button = (Button) findViewById (R. id. nextButton1 );
Pre1Button. setOnTouchListener (this );
Next1Button. setOnTouchListener (this );
GestureDetector = new GestureDetector (this );
ViewFlipper = (ViewFlipper) this. findViewById (R. id. ViewFlipper );
}
 
// 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): The events in the DoubleTap gesture are notified, including the down, up, and move events (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): it is used to determine whether a click is SingleTap rather than DoubleTap. If a click is made twice consecutively, It is a DoubleTap gesture. If only one click is made, after waiting for a while, the OPhone system determines that the second click is SingleTap rather than DoubleTap, and then triggers the SingleTapConfirmed event.
Public boolean onDoubleTap (MotionEvent e ){
If (viewFlipper. isFlipping ()){
ViewFlipper. stopFlipping ();
} Else {
ViewFlipper. startFlipping ();
}
Return true;
}
 
@ Override
Public boolean onTouchEvent (MotionEvent event ){
Return this. gestureDetector. onTouchEvent (event );
}
 
@ Override
Public boolean onDown (MotionEvent e ){
// Down event
Return false;
}
 
Public boolean onFling (//
MotionEvent e1, // e1: The first ACTION_DOWN event (the point that the finger presses)
MotionEvent e2, // e2: The last ACTION_MOVE event (the point at which the finger is released)
Float velocityX, // velocityX: the speed at which the finger moves on the X axis. Unit: pixel/second.
Float velocityY) // velocityY: the speed at which the finger moves on the Y axis. Unit: pixel/second.
{
// Sliding gesture event
If (e1.getX ()-e2.getX ()> 60) {// slide to the right, next page
// SetOutAnimation: Specifies the animation used when the View exits the screen. The setInAnimation function is the same.
This. viewFlipper. setInAnimation (AnimationUtils. loadAnimation (this,
R. anim. zoomin ));
This. viewFlipper. setOutAnimation (AnimationUtils. loadAnimation (this,
R. anim. zoomout ));
This. viewFlipper. showNext (); // call this function to display the next View in FrameLayout.
Return true;
} Else if (e1.getX ()-e2.getX () <-60) {// slide to the left, previous
This. viewFlipper. setInAnimation (AnimationUtils. loadAnimation (this,
R. anim. zoomin ));
This. viewFlipper. setOutAnimation (AnimationUtils. loadAnimation (this,
R. anim. zoomout ));
This. viewFlipper. showPrevious (); // call this function to display the previous View in FrameLayout.
Return true;
}
Return false;
}
 
@ Override
Public void onLongPress (MotionEvent e ){
// Long press event
}
 
@ Override
Public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX,
Float distanceY ){
// Drag the event on the screen.
Return false;
}
 
@ Override
Public void onShowPress (MotionEvent e ){
// When a down event occurs, move or the up event is not triggered before the event is triggered;
}
 
@ Override
Public boolean onSingleTapUp (MotionEvent e ){
// Click the up event once;
Return false;
}
 
// Button touch event
Public boolean onTouch (View v, MotionEvent event ){
Switch (v. getId ()){
Case R. id. preButton1:
If (event. getAction () = MotionEvent. ACTION_DOWN) {// click the background image
// Pre1Button. setBackgroundResource (R. drawable. pre_button1 );
}
// After the button is up, set the background image and slide to the previous page.
Else if (event. getAction () = MotionEvent. ACTION_UP ){
// Pre1Button. setBackgroundResource (R. drawable. pre_button );
// Flipper. setInAnimation (AnimationUtils. loadAnimation (Main. this,
// R. anim. push_right_in ));
//
// Flipper. setOutAnimation (AnimationUtils. loadAnimation (Main. this, R. anim. push_right_out ));
ViewFlipper. showPrevious ();
}
Break;
Case R. id. nextButton1:
If (event. getAction () = MotionEvent. ACTION_DOWN ){
// Next1Button. setBackgroundResource (R. drawable. next_button1 );
}
// Set the background image after the button is up and slide to the next page
Else if (event. getAction () = MotionEvent. ACTION_UP ){
// Next1Button. setBackgroundResource (R. drawable. next_button );
// Flipper. setInAnimation (AnimationUtils. loadAnimation (Main. this,
// R. anim. push_left_in ));
//
// Flipper. setOutAnimation (AnimationUtils. loadAnimation (Main. this, R. anim. push_left_out ));
ViewFlipper. showNext ();
}
Break;
 
Default:
Break;
}
Return false;
}
}
 
Xml Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<AbsoluteLayout 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/ViewFlipper"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent">
<! -- Page 1st -->
<AbsoluteLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: background = "# FFFFFF" android: layout_height = "fill_parent">
<TextView android: text = "1st page" android: textSize = "35dp"
Android: textColor = "#000000" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: layout_x = "115dp"
Android: layout_y = "20dp"/>
</AbsoluteLayout>
<! -- Page 2nd -->
<AbsoluteLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: background = "# FFFF00" android: layout_height = "fill_parent">
<TextView android: text = "2nd page" android: textSize = "35dp"
Android: textColor = "#000000" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: layout_x = "120dp"
Android: layout_y = "20dp"/>
</AbsoluteLayout>
<! -- Page 3rd -->
<AbsoluteLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: background = "#99CC33" android: layout_height = "fill_parent">
<TextView android: text = "3rd page" android: textSize = "35dp"
Android: textColor = "#000000" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: layout_x = "120dp"
Android: layout_y = "20dp"/>
</AbsoluteLayout>
</ViewFlipper>
<Button android: layout_width = "wrap_content" android: text = "Previous Page"
Android: gravity = "center" android: textSize = "20sp"
Android: layout_height = "40dp" android: id = "@ + id/preButton1"
Android: layout_x = "101dp" android: layout_y = "300dp"/>
<Button android: layout_width = "wrap_content" android: text = "next page"
Android: layout_height = "40dp" android: id = "@ + id/nextButton1"
Android: gravity = "center" android: textSize = "20sp" android: layout_x = "182dp"
Android: layout_y = "300dp"/>
</AbsoluteLayout>

Author "AndLi"
 

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.