Convert and get coordinates between Android DP and PX

Source: Internet
Author: User
Tags gety

DP, PX, SP conversion

  1. 1 Public class Densityutil {2     /** 3 * Convert PX value to dip or DP value to ensure size unchanged4      *  5 * @param pxvalue6 * @param Scale7 * (attribute density in Displaymetrics Class)8      */  9 public static int Px2dip (float pxvalue, float scale) {ten return (int) (Pxvalue/scale + 0.5f); One} A    -     /**  - * Convert dip or DP values to PX values to ensure size is constant the      *   - * @param dipvalue - * @param Scale - * (attribute density in Displaymetrics Class) +      */  Public  Static int dip2px (float dipvalue, float scale) {+ RETURN (int) (Dipvalue * scale + 0.5f); + }   at    -     /**  - * Convert PX value to SP value, keep text size unchanged -      *   - * @param pxvalue - * @param fontscale in * (Attribute scaleddensity in Displaymetrics Class) -      */  Public  Static int px2sp (float pxvalue, float fontscale) {return (int) (Pxvalue/fontscale + 0 .5f); + }   the    *     /**  $ * Convert SP value to PX value, keep text size unchangedPanax Notoginseng      *   - * @param spvalue the * @param fontscale + * (Attribute scaleddensity in Displaymetrics Class) A      */  Public  Static int sp2px (float spvalue, float fontscale) {[] (int) (Spvalue * Fontscale + 0.5f); /+ }   $}

But why do we have to add 0.5f at the end?

According to normal reasoning should be dip = pxvalue/scale and px = dipvalue * scale,

In fact, the exact value should be our reasoning, the reason behind the addition of 0.5f is because we want is not so accurate, according to the reasoning is a floating point, and we generally use int type is enough, here involves a type conversion accuracy problem, familiar with Java effect of the classmate should know

A float of type 4.1 and 4.9 turns into an int type, loses precision to 4 of int type, and if we want to round, add 0.5f to them, and the result is:

4.4 + 0.5 = 4.9 to int or 4, and 4.5 + 0.5 = 5.0 to int is 5, just round, which guarantees that the values we calculate are relatively accurate.

Android coordinates

There are two coordinate systems in Android, called the Android coordinate system and the view coordinate system, respectively. And there are some related ways to get the coordinates of the

The coordinate value. Only by figuring out the differences can you achieve them without making mistakes or getting the results you want.

One, Android coordinate system and view coordinate system

(1) Android coordinate system

Let's take a look at what the Android coordinate system looks like. Paste a picture to illustrate, as follows:

As shown, the Android coordinate system takes the top-left corner of the phone screen as the coordinate origin, from that point to the right x-axis positive direction, and from that point down to the y-axis positive direction. In touch events, use the GETRAWX () and Getrawy () methods

The coordinates obtained are the coordinate values under the standard of this coordinate system.

(2) View coordinate system

Another coordinate system in Android is called the View coordinate system, which describes the location of the child view in the parent view. One more picture to illustrate, as follows:

As shown, the view coordinate system is in the upper-left corner of the parent view as the origin of the coordinates. The corresponding Origin points to the right of the x-axis positive direction, and the origin down to the y-axis positive direction. In touch, the

The coordinate values obtained by GetX () and gety () are the coordinate values in the view coordinate system.

The methods of obtaining coordinate values and relative distances

In Android, there is a very rich way to get coordinate values and relative distances. However, when using these methods, it is important to understand which coordinate system is used to obtain the standard.

To illustrate these methods one by one, I have also prepared a picture, as follows:

The above diagram is a good illustration of where the coordinate values or distances from each method go. Note that there are three black boxes in the picture, the outermost one is the phone screen,

The middle tier is ViewGroup, and the inner layer is the view placed in the ViewGroup.

In fact, the method can be divided into two categories, one is the view provided by the method, a class is the method provided by Motionevent. The following are explained separately:

View provides methods for obtaining coordinates and distances:

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

Motionevent provides the method:

GetX () Gets the distance to the left of the Click event Distance control, which is the view coordinate

GetY () Gets the distance from the top edge of the Click event Distance control, which is the view coordinate

GETRAWX () Gets the distance from the left side of the entire screen of the Click event, which is the absolute coordinate

Getrawy () Gets the distance of the click event from the top edge of the entire screen, which is the absolute coordinate

Convert and get coordinates between Android DP and PX

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.