Android Gesture Recognition (sliding left and right) enables page switching and android gestures

Source: Internet
Author: User

Android Gesture Recognition (sliding left and right) enables page switching and android gestures

</Pre> to achieve the effect of Sliding between the left and right of the page, the gesture reader must be the GestureDetector object. <P> </p> <p> the usage is actually very simple. It is easy to find it later. </P> <p> the procedure is 123 .. </P> <p> 1. initialize the gesture reader </p> <p> 2. register the touch event of gesture recognition. </P> <p>. The recognition gesture page can be switched in only two steps. </P> <p> because page switching is available in every activity, we put the initial registration of the gesture reader in baseactivity to make the most effective use of our code.. </P> <pre code_snippet_id = "673269" snippet_file_name = "blog_20150523_2_2749063" name = "code" class = "java"> package com. example. gesturedemo; import android. app. activity; import android. OS. bundle; import android. view. gestureDetector; import android. view. view; import android. view. motionEvent; public abstract class BaseActivity extends Activity {private GestureDetector mGestureDetector; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); // 1 initialize mGestureDetector = new GestureDetector (this, new GestureDetector. simpleOnGestureListener () {@ Overridepublic boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {// e1: position 1 pressed e2 position when the hand leaves the screen position velocityX speed along the x axis velocityY: speed along the Y axis // determine the size of moving vertically if (Math. abs (e1.getRawY ()-e2.getRawY ()> 100) {// Toast. makeText (getApplicationContext (), "The action is invalid", 0 ). show (); return true;} if (Math. abs (velocityX) <150) {// Toast. makeText (getApplicationContext (), "moving too slowly", 0 ). show (); return true;} if (e1.getRawX ()-e2.getRawX ()> 200) {// slide to the right to display next page next (null ); return true;} if (e2.getRawX ()-e1.getRawX ()> 200) {// slide to the left to indicate the previous page // display the previous page pre (null); return true; // consume the current event to prevent the current event from being passed down} return super. onFling (e1, e2, velocityX, velocityY) ;}});}/*** next page * @ param view */public abstract void next (View view ); /*** Previous Page ** @ param view */public abstract void pre (View view); // rewrite the activity's touch event @ Overridepublic boolean onTouchEvent (MotionEvent event) {// 2. enable the gesture reader to take effect of mGestureDetector. onTouchEvent (event); return super. onTouchEvent (event );}}


Then let each specific interface inherit the baseactivity and implement the corresponding next or pre method. To display the effects, we define several animated effects: left-to-right or right-to-left. For more information, see the demo.




It's really easy (click to download)



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.