Android uses activity to monitor fingers up and down to slide _android

Source: Internet
Author: User
Tags gety

Using the Ontouchevent method of activity to realize the monitoring of fingers sliding up and down

Using the activity of the Ontouchevent method to monitor the finger click events, the fingers will slide when the first press, slide down another place to lift, we can according to the coordinates and the lifting coordinates to calculate the user is in which direction to slide.

Package com.example.testtt;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.MotionEvent;

Import Android.widget.Toast;
 public class Mainactivity extends activity {//finger pressed point for (x1, y1) finger left screen for (x2, y2) float x1 = 0;
 float x2 = 0;
 float y1 = 0;
 
 float y2 = 0;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_main); @Override public boolean ontouchevent (Motionevent event) {//Inherits the Ontouchevent method of the activity, directly listening to the Click event if (event.getactio
  N () = = Motionevent.action_down) {///when the finger is pressed x1 = Event.getx ();
 Y1 = Event.gety ();
  if (event.getaction () = = motionevent.action_up) {//When the finger leaves x2 = event.getx ();
  y2 = Event.gety ();
  if (Y1-y2 >) {toast.maketext (mainactivity.this, "Up slippery", toast.length_short). Show ();
  else if (Y2-y1 >) {toast.maketext (mainactivity.this, "downward", Toast.length_short). Show (); else if (X1-x2 >) {toast.maketext (M)Ainactivity.this, "slide to the left", Toast.length_short. Show ();
  else if (X2-x1 >) {toast.maketext (mainactivity.this, slide right, Toast.length_short). Show ();
 } return Super.ontouchevent (event);

 }
 
 
}


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.