Android swipe gesture triggers and does not increase the layout level to enlarge the click Area

Source: Internet
Author: User

Recent projects need to implement gesture swipe or click the effect of sliding out of the interface, the implementation is to use Gesturedetector, and then in the onfling, but encountered a problem: gesture swipe is for the entire layout, but if there is a separate view settings click Listen, The swipe on the view will fail.

If the view itself is dedicated to click, then there is no problem, unfortunately, this view is not small size, so the need to expand the area of the click.

The first thing we can think of is that the view is wrapped up in a container, like relativelayout,linearlayout, and so on, but the level of the layout will be added one more layer.

Can we think of a way to expand the click area without adding layers?

Gesturedetector is the gesture action class provided by Android, which provides a click, double-click, long-Press, and so on, and passes these events to Ontouch, so we can use this to determine which control is triggering the Click event.

But our problem is to expand the area of the click rather than identify the control, so we need to know which area the user's click action is in.

Fortunately, Motionevent provides the coordinates at the time of the event, so that we can know the coordinates at the time of the click and further determine if the area is within the specified range.

Motionevent is a critical class for touch-aspect coding in Android, which provides user touch information on the screen. We can get touch event types, touch coordinates, and now support multi-touch with motionevent, as well as further access to touch index information.

Let's start with the code:

1 @Override2      Public BooleanOnTouch (View V, motionevent event) {3         if(event.getaction () = =Motionevent.action_down) {4             intDownx = (int) Event.getx ();5             int[] location =New int[2];6 Ivmore.getlocationinwindow (location);7             if(Math.Abs (downx-location[0]) <= 50){8                  ...9             }Ten         } One         returngesturedetector.ontouchevent (event); A}

We first get to the touch event type through Motionevent's getaction, click the type Action_down, and then get to the x-coordinate of the click by Motionevent getx, The coordinates of the specified view are then obtained through Getlocationinwindow, where the x-axis coordinates of the component are specified plus or minus 50 (representing the left and right extents) within the trigger range.

We can now expand the range of clicks without increasing the layout level.

It is very simple to use the gesturedetector to realize the upper slide, as long as the Ongesturelistener interface is implemented and the operation we specify in the corresponding callback is performed, but because Gesturedetector itself does not capture touch events, So to implement the Ontouch event again, and call Gesturedetector ontouchevent, the corresponding event passed.

The action of the swipe is made in the onfling callback, which passes through the motionevent of the two histories, as long as we determine whether their y-axis coordinates are changed.

Android Touch is a fairly large range of topics, as long as we define our own needs, to find the corresponding quick solution.

Android swipe gesture triggers and does not increase the layout level to enlarge the click Area

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.