Compared with the effect of the long press and click events appearing on the android2.x version, it has to be said that the sliding operation has a better user experience. As a result, more sliding operations have been seen since the Android 4.X version. More and more third-party applications imitate such effects to improve their applications. In this paper, we investigate the formation of sliding effect.
How the slide effect produces
The essence of sliding a view is to move a view. is to change its coordinate position, its principle and animation produced effect principle should be the same. Its ultimate essence is to change its coordinates. So, to realize the view sliding effect, we just need to monitor the user's touch events and dynamically change the view coordinates.
Here, on the listener touch event, change the view coordinates. We therefore want to understand the window coordinate system and screen touch event motionevent.
Android coordinate system
Why there is sliding one said, the physics of Junior High School tells us, is relative to the coordinate system took place the movement. In Android, we set the top left corner of the screen to be the coordinate origin, from this point to the right, to the positive direction of the x-axis, downward to the positive direction of the Y axis. As shown in the figure:
In addition to this system coordinates, there is also a view system. Similar to system coordinate, so that it is also a positive direction with the origin downward to the y-axis, with the origin to the right of the x axis. However, the origin of this coordinate is the upper-left corner of the parent control. As shown in the figure:
We understand the coordinate system, and we understand the touch event--motionevent.
Don't underestimate this motionevent event, in the process of interacting with the user, this dongdong is essential. First, look at him. Encapsulates those constants that represent different event types, as follows:
Single Point Touch Property press event public
static final int Action_down = 0;
Single Point touch property lift event public
static final int action_up = 1;
The touch point moves the event public
static final int Action_move = 2;
The touch action cancels public
static final int Action_cancel = 3;
Touch Cross-border Event public
static final int action_outside = 4;
Multi-Touch Press event public
static final int Action_pointer_down = 5;
Multi-Touch Lift event public
static final int action_pointer_up = 6;
In general, we use this template method to listen to the ontouchevent (Motionevent ev) method and then use the Switch-case method for judgment processing, which are all fixed formats and the following code:
Switch (event.getaction ()) {
//Touch Press event case
Motionevent.action_down: Break
;
Touch Move state case
Motionevent.action_move: Break
;
Touch Lift State case
motionevent.action_up: Break
;
}
Generally, do not involve multi-touch operations, the general use of this code template can complete the corresponding event monitoring.
In addition, the Android system provides many ways to get coordinate values and relative distances. To our beginners to bring little confusion, here a picture let you understand. As shown in the figure:
Various ways to explain:
GetLeft ()--Gets the distance from the left side of the view itself to the left of its parent control.
GetRight ()--Gets the distance from the right of the view itself to the left of its parent control.
GetTop ()--Gets 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 of the click event from the left side of the control, which is the view coordinate.
GetY ()--Gets the distance of the click event from the top of the control, which is the view coordinate.
GETRAWX ()--Gets the distance of the click event from the left side of the control screen, that is, absolute coordinates.
Getrawy ()--Gets the distance of the click event from the top of the screen, that is, absolute coordinates.
This is a knowledge supplement to the sliding effect.
The above is the entire content of this article, I hope to help you learn.