The view coordinate system developed by Android (the method of view acquiring its own coordinates and the acquisition of coordinates in the Click event)

Source: Internet
Author: User
Tags gety

When doing a view background effect is the coordinates of the various methods to get dizzy, a few copied to copy the blog has not been very clear.

Now summarize the whole thing.

In fact, as long as the following picture to see clearly there is no problem.

There are several methods involved:

View gets its own coordinates: GetLeft (), GetTop (), GetRight (), Getbottom ()

View gain self-width: getheight (), getwidth ()

Motionevent Get Coordinates: GetX (), GetY (), GETRAWX (), Getrawy ()


The first is a few methods of view,

The two methods of obtaining their own wide height are clear, needless to say, getting the coordinates of these few is a bit confusing.

According to the above diagram should be easier to understand, the picture on the screen put a viewgroup layout, there is a view control

GetTop: Gets the distance from the top edge of the view itself to the top edge of its parent layout

GetLeft: Gets the distance from the left side of the view itself to the left side of its parent layout

GetRight: Gets the distance from the right side of the view itself to the left side of its parent layout

Getbottom: Gets the distance from the bottom of the view itself to the top edge of its parent layout

Where can the data obtained by these methods be used?
For example, to implement a custom special layout like http://blog.csdn.net/singwhatiwanna/article/details/42614953
Here is a water ripple effect layout that will ripple when clicked on any control within the layout.
Then after clicking on a control in the layout, it is necessary to update the ripple radius of the control by constantly refreshing the layout, in order to save resources, each time the layout is refreshed, the layout will not be refreshed, but only through

Postinvalidatedelayed (Invalidate_duration, left, top, right, bottom);  

In the layout of the canvas each time only to update the Click event click on the corresponding control of the location, then here you can use the view of the four methods, respectively, to obtain their own four edges corresponding coordinates

This allows the layout to refresh the redraw.

Of course, the blog is to use absolute coordinates to calculate, because here is the implementation of a layout, there may also be nested inside another layout, after many nested after the obtained value, is relative to the control directly corresponding to the parent layout (this layout may already be the layout of our rewritten sub-layout) distance, So the area to be refreshed is definitely inaccurate, so the blog uses relative screen absolute coordinates to calculate the area of the control that needs to be refreshed.

If the custom is not the layout, but just a control, you can get the coordinates by the above method, and then ask your own layout to redraw the area. Of course, this is only a way of thinking, in fact, there is no need to ask for a redraw layout, can be directly view itself to redraw.


Then there is the method of Motionevent:

GetX (): Gets the x-axis coordinates of the left side of the Click event relative to the control, that is, the distance from the left of the control

GetY (): Gets the y-axis coordinates of the top edge of a click event relative to the control, that is, the distance from the top edge of the control

GETRAWX (): Gets the x-axis coordinates of the click event relative to the left of the entire screen, that is, the distance from the left of the entire screen

Getrawy (): Gets the y-coordinate of the Click event relative to the top edge of the entire screen, that is, the distance of the click event from the top edge of the screen

Where can these methods be used?

GETRAWX and Getrawy are widely used in the previous blog, and can go there to see usage, GetX () and gety () are more likely to be used when customizing the view.

Then there is a blog to write the first implementation of the effects, you can look at the following is the link.

Link placeholder

jason0539

Weibo: http://weibo.com/2553717707

Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)


The view coordinate system developed by Android (the method of view acquiring its own coordinates and the acquisition of coordinates in the Click event)

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.