Android Development using Gesturedetector gesture sliding interface

Source: Internet
Author: User

People accustomed to Apple know that in the app only need to swipe left and right to make the page conversion, but most of the software in Android is only using the return key to change the interface, in fact, Android has long ago to support gestures to update the operation, Just a lot of apps are not used to gesturedetector, the following simply said through the gesturedetector to the interface, the following only give the key code, directly use.


Gesturedetector detector = new Gesturedetector (This, new Gesturedetector.ongesturelistener () {//
@Override
public boolean Ondown (Motionevent e) {
return false;
}

@Override
public void Onshowpress (Motionevent e) {

}

@Override
public boolean onsingletapup (Motionevent e) {
return false;
}

@Override
public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {
return false;
}

@Override
public void Onlongpress (Motionevent e) {

}
@Override
public boolean onfling (Motionevent E1, motionevent E2, float Velocityx,
Float velocityy) {
if (E1.getx ()-e2.getx () > 120) {//sliding left, where you can update the interface, you do whatever you want.
Intent Intent = new Intent ();
Intent.setclass (Mainactivity.this, Two.class);
StartActivity (Intent);
return true;
else if (E1.getx ()-E2.getx () <-120) {//Slide right
MainActivity.this.finish ();
return true;
}
return false;
}
});

@Override
public boolean ontouchevent (Motionevent event) {//must rewrite the Ontouchevent method in the activity in order for the gesture to take effect
Detector.ontouchevent (event);
Return Super.ontouchevent (event);
}

Above is the normal sliding, when you in ListView and so on itself with sliding control using gestures will definitely conflict, the following only gives the simplest and most brutal solution-change the order of the system's Ontouchevent events, rewrite the Dispatchtouchevent method

@Override
public boolean dispatchtouchevent (Motionevent ev) {
This.detector.onTouchEvent (EV);
return super.dispatchtouchevent (EV);
}

The above is through the gesturedetector gesture to the interface between the transformation, the animation study of the children's shoes can be in the interface alternately with animation effect! Gesturedetector also has a lot of features that are interesting to continue to study

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.