Android provides the gesturedetector class to process some commonly used gesture operations, such as onlongpress and onfling. However, gesturedetector is not used here, but is directly processed in the ontouchevent of the custom view rewriting.
To achieve this, if the mobile phone does not move (for example, 500 ms) within the specified time when holding down the screen, it enters the long-press mode, in this case, moving your finger on the screen is counted as a long press. If you press and hold the screen on your phone, you can move the screen immediately.
The motionevent class provides functions for recording the current coordinates (getx (), Gety (), geteventtime (), and getdowntime ()). Motionevent also provides the current operation type, such as pressing (action_down), moving (action_move), and popping (action_up ). With these parameters, we can easily achieve the desired effect.
The general idea is as follows: record the X, Y coordinates, and press time when you press the button. Obtain the moving time when you move the button for the first time. If it is longer than the specified long press time, enter the long press mode, otherwise, it is a normal moving mode. It is easy to achieve this effect in the simulator, but this effect cannot be achieved when running in the real machine. When the simulator clicked, it was guaranteed that action_move would not be triggered without moving the mouse, but the real machine was very sensitive. action_move would not be triggered until several milliseconds after action_down. After thinking about it, you only need to change it slightly to achieve the same effect on the real machine. It is to determine whether the Offset Value of the coordinates after action_move and the coordinates of action_down is smaller than the specified offset pixel. If it is within the specified value, it is considered that it is not moved. So we have the following function.
/***** Determine whether there is a long press * @ Param lastx x coordinate when pressed * @ Param lasty y coordinate when pressed *** @ Param thisx * x coordinate when moved ** @ Param thisy * Y coordinate when moving ** @ Param lastdowntime * press time ** @ Param thiseventtime * move time ** @ Param longpresstime * to determine the threshold value of a long time */static Boolean islongpressed (float lastx, float lasty, float thisx, float thisy, long lastdowntime, long thiseventtime, long longpresstime) {float offsetx = math. ABS (thisx-lastx); float offsety = math. ABS (thisy-lasty); long intervaltime = thiseventtime-lastdowntime; If (offsetx <= 10 & offsety <= 10 & intervaltime> = longpresstime) {return true ;} return false ;}
When action_down, record lastx, lasty, and lastdowntime. When action_moveDetermine whether the current mode is long-pressed (class flag variable Mode)), If not, obtain the current thisx, thisy, and thiseventtime to call the function for judgment. Finally, do not forget to set the long-pressed flag value to false in action_up. Action_down:
// Check whether long-pressed, if (! Mislongpressed) {mislongpressed = islongpressed (mlastmotionx, mlastmotiony, X, Y, lastdowntime, eventtime, 500);} If (mislongpressed) {// what is done in long-pressed mode} else {// what is done in Mobile mode }}