Android up and down gesture judging according to other people's changes

Source: Internet
Author: User
Tags gety

Gestureutils.java

PackageCom.gesture;

ImportAndroid.content.Context;
ImportAndroid.util.DisplayMetrics;
ImportAndroid.view.WindowManager;

Public Classgestureutils {



//Get the size of the screen
Public StaticScreen Getscreenpix (context context) {
Displaymetrics DM= NewDisplaymetrics ();
WindowManager WindowManager=(WindowManager) Context.getsystemservice (Context.window_service);
Windowmanager.getdefaultdisplay (). Getmetrics (DM);
Return NewScreen (dm.widthpixels,dm.heightpixels);
}

Public Static Classscreen{

Public IntWidthpixels;
Public IntHeightpixels;

PublicScreen () {

}

PublicScreen (IntWidthpixels,IntHeightpixels) {
This. widthpixels=Widthpixels;
This. heightpixels=heightpixels;
}

@Override
public String toString () {
Span style= "color: #0000ff;" >return ( "+widthpixels+ "," +heightpixels+) Span style= "color: #000000;" > ";
}

}

} /span>

Builegestureext.java

PackageCom.gesture;

ImportCom.gesture.GestureUtils.Screen;

ImportAndroid.content.Context;
ImportAndroid.view.GestureDetector;
ImportAndroid.view.MotionEvent;

Public ClassBuilegestureext {
Public Static Final IntGesture_up=0;
Public Static Final IntGesture_down=1;
Public Static Final IntGesture_left=2;
Public Static Final IntGesture_right=3;
PrivateOngestureresult Ongestureresult;
PrivateContext Mcontext;
PublicBuilegestureext (Context c,ongestureresult ongestureresult) {
This. mcontext=C
This. ongestureresult=Ongestureresult;
Screen=Gestureutils.getscreenpix (c);
}
PublicGesturedetector Buile ()
{
Return NewGesturedetector (Mcontext, Ongesturelistener);
}

PrivateScreen screen;
PrivateGesturedetector.ongesturelistener Ongesturelistener= NewGesturedetector.simpleongesturelistener () {

@Override
Public BooleanOnfling (motionevent E1, motionevent E2,FloatVelocityx,
FloatVELOCITYY) {
FloatX=E2.getx ()-E1.getx ();
FloatY=E2.gety ()-E1.gety ();
//The limit must be 1/4 across the screen to be counted.
FloatX_limit=Screen.widthpixels/ 4;
FloatY_limit=Screen.heightpixels/ 4;
FloatX_abs=Math.Abs (x);
FloatY_abs=Math.Abs (y);
If(X_abs>=Y_abs) {
//Gesture left or right
If(x>X_limit||X< -X_limit) {
If(x>0){
//Right
Doresult (Gesture_right);
}Else If(x<=0){
//Left
Doresult (Gesture_left);
}
}
}Else{
//Gesture down or up
If (y > Y_limit || y < -y_limit) {
if (y< Span style= "color: #000000;" >>0 //down Doresult (Gesture_down);} else if (y<=0) {//up doresult (gesture_up);}} } return true; } }; public void Doresult (int result) {if (ongestureresult!=null) {Ongestureresult.ongestureresult (Result),}} public Interf Ace Ongestureresult {public void ongestureresult (int direction);}

Demo

package com.gesture; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.widget.Toast; public class GesturetestActivity extends Activity {      /** Called when the activity is first created. */      private GestureDetector gestureDetector;       @Override      public void onCreate(Bundle savedInstanceState) {          super .onCreate(savedInstanceState);          setContentView(R.layout.main);          gestureDetector = new BuileGestureExt( this , new BuileGestureExt.OnGestureResult() {              @Override              public void onGestureResult( int direction) {                  show(Integer.toString(direction));              }          }          ).Buile();      }      @Override      public boolean onTouchEvent(MotionEvent event) {          return gestureDetector.onTouchEvent(event);      }      private void show(String value){          Toast.makeText( this , value, Toast.LENGTH_SHORT).show();      }      }
Related Article

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.