Andropid custom components-coordinate details, andropid details

Source: Internet
Author: User

Andropid custom components-coordinate details, andropid details

When I made a special view background effect, I was dizzy by the various methods of obtaining coordinates. I didn't quite figure out the blog I copied.

Now let's summarize the entire process.

As a matter of fact, you just need to take a look at the figure below.

The methods involved include the following:

View to get its own coordinates: getLeft (), getTop (), getRight (), getBottom ()

View to get its own width and height: getHeight (), getWidth ()

MotionEvent: getX (), getY (), getRawX (), getRawY ()


The first is the view method,

The two methods for obtaining their own width and height are very clear. Needless to say, it is a bit confusing to obtain the coordinates.

According to the figure above, it should be easier to understand. A ViewGroup layout is displayed on the screen, which contains a View control.

GetTop: obtains the distance from the top edge of the view to the top edge of its parent layout.

GetLeft: the distance from the left of the view to the left of its parent layout.

GetRight: Get the distance from the right of the view to the left of its parent Layout

GetBottom: obtains the distance from the bottom edge of the view to the top edge of its parent layout.

Where can the data obtained by these methods be used?
For example, to implement a customized special layout, like a http://blog.csdn.net/singwhatiwanna/article/details/42614953
What we need to achieve here is a water ripple special effect layout. After clicking any control in the layout, the ripple effect will appear.
After you click a widget in the layout, you must update the ripple radius on the widget by refreshing the layout. To save resources, every time you refresh the layout, the entire layout is not refreshed.

[Java]View plaincopyprint?
  1. PostInvalidateDelayed (INVALIDATE_DURATION, left, top, right, bottom );

On the canvas of the layout, only update the position of the corresponding control clicked by the click event each time. Here we can use the four methods of view, obtain the coordinates of the four edges respectively.

To refresh the layout.

Of course, the blog uses absolute coordinates for calculation, because a layout is implemented here, and other la s may be nested in it, and the values obtained after multiple nesting are obtained, it is the distance from the parent layout directly corresponding to the control (this layout may already be the child layout of the layout we have rewritten), so the refresh area is definitely not accurate, therefore, the blog uses the absolute coordinates of the screen to calculate the control area to be refreshed.

If the custom layout is not a control, you can obtain the coordinates through the above method, and then ask your layout to re-paint the area. Of course, this is just a way of thinking. In fact, there is no need to re-paint the layout. You can simply re-map the view itself.


Then the motionEvent method:

GetX (): Get the X axis coordinate of the click event to the left of the control, that is, the distance between the Click Event and the left of the control.

GetY (): obtains the Y axis coordinate of the Click Event relative to the top edge of the control, that is, the distance between the Click Event and the top edge of the control.

GetRawX (): obtains the X axis coordinate of a click event relative to the left side of the screen, that is, the distance between a click event and the left side of the screen.

GetRawY (): obtains the Y coordinate of the Click Event relative to the top edge of the screen, that is, the distance between the Click Event and the top edge of the screen.

Where can these methods be used?

GetRawX and getRawY are widely used in the previous blog. You can check their usage, getX () and getY () these two methods may be used more when customizing the view.

After that, you will have a special effect for blog writing at the beginning. You can see the following link.

View and view group Rewriting

Author: jason0539

Weibo: http://weibo.com/2553717707

Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.