The swipe operation has a better user experience than the effect of a long press or click event that appears on the android2.x version. So, starting with the Android 4.X version, there are more swipe operations. More and more third-party applications mimic such effects to improve their applications. In this paper, we are inquiring into the effect of sliding.
How the slide effect is generated
Sliding a view is essentially moving a view. is to change its coordinate position, its principle and animation produced by the effect principle should be the same. Its ultimate essence is to change its coordinates. So, to achieve the effect of the view swipe, we just need to listen to the user's touch events and dynamically change the view coordinates.
Here, it says to listen for touch events and change the view coordinates. We therefore want to understand the window coordinate system and the screen touch event motionevent.
Android coordinate system
Why would there be sliding one said that the physics of junior High school tells us that it is relative to the coordinate system that the movement has taken place. In Android, we set the upper-left corner of the screen as the origin of the coordinates, from this point to the right in the positive direction of the x-axis, and down to the positive direction of the y-axis. :
In addition to the system coordinates, there is a view coordinate system. Similar to system coordinate systems, to the positive direction of the y-axis from the origin to the right, and the positive direction of the x-axis at the origin. Only, the origin of this coordinate is the upper-left corner of the parent control. :
We learned about the coordinate system, and here we understand touch event--motionevent.
Don't underestimate this motionevent event, in the process of interacting with the user, this stuff is necessary. First, let's see that he encapsulates those constants that represent different event types, as follows:
//Single Touch Property press event Public Static Final intAction_down = 0; //Single Touch Property Lift event Public Static Final intACTION_UP = 1; //Touch Point Move Event Public Static Final intAction_move = 2; //Touch Action Cancel Public Static Final intAction_cancel = 3; //touch out of bounds events Public Static Final intAction_outside = 4; //Multi-Touch Press event Public Static Final intAction_pointer_down = 5; //Multi-Touch Lift event Public Static Final intACTION_POINTER_UP = 6;
Typically, we will use this template method to listen to the ontouchevent (motionevent ev) method, and then use the Switch-case method for judgment processing, these are fixed format, the code is as follows:
Switch(Event.getaction ()) {//Touch Press Event CaseMotionevent.action_down: Break;//Touch Move State CaseMotionevent.action_move: Break;//Touch Lift State Casemotionevent.action_up: Break; }
The general situation, does not involve multi-touch operation, generally with this code template can complete the corresponding event monitoring.
In addition, the Android system provides many ways to obtain coordinate values and relative distances. There is little confusion for beginners, here is a picture that you can understand. :
Various ways to explain:
GetLeft ()--Gets the distance from the left side of the view itself to the left side of its parent control.
GetRight ()--Gets the distance from the right side of the view itself to the left side of its parent control.
GetTop ()--Gets the distance from the top of the view itself to the top of its parent control.
Getbottom ()--Gets the distance from the bottom of the view itself to the top of its parent control.
GetX ()--Gets the distance from the left edge of the Click event Distance control, which is the view coordinate.
GetY ()--Gets the distance from the top of the control on the Click event, which is the view coordinate.
GETRAWX ()--Gets the distance from the left side of the control screen of the Click event, which is the absolute coordinates.
Getrawy ()--Gets the distance of the click event from the top of the screen, which is the absolute coordinate.
This is a knowledge complement to the sliding effect. I Caishuxueqian, I beg to spit groove.
Android Scroll ANALYSIS--sliding effect generation