Custom control to thoroughly grasp, master the Android coordinate system and some API coordinate meaning is no doubt can not be ignored skills, for the control position, touch point, control drawing are inseparable from the coordinate system, so learning custom control before we will first talk about the Android coordinate system.

First, Android screen coordinate system and mathematical coordinate system difference

(1) In the mathematical coordinate system, the intersection point of xy axis is taken as the origin of coordinates, x axis is positive to the right and Y axis is positive, which is already familiar to children, as shown in the figure:

(2) There is still a difference between the coordinate system on the mobile phone screen and the mathematical coordinate system. In mobile devices, the upper left corner of the screen is generally defined as the origin of coordinates, X-axis to the right is the positive direction, Y-axis down is the positive direction, as shown in the figure:

Second, the division of Android screen area

The Android screen area is mainly divided into five areas: status bar area, ActionBar area, View layout area, App App area and screen area, and there is nesting relationship between each other. As shown in the figure:

Let’s take a look at the height of each region:

(1) Status bar area height acquisition:

Rect Rect = new Rect(); Rect = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); int statusBarHeight = rect.top; Resources Resources = this.getResources(); int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android"); int height = resources.getDimensionPixelSize(resourceId); Int statusHeight = -1; int statusHeight = -1; try { Class clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int intheight = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); StatusHeight = this.getResources().getDimensionPixelSize(intheight); } catch (Exception e) { e.printStackTrace(); }Copy the code

(2) Height acquisition of ActionBar region:

Int actionBarHeight = getSupportActionBar().getheight (); int actionBarHeight = getActionBar ().getheight (); TypedValue TV = new TypedValue(); if (this.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { int actionBarHeightOther = TypedValue.complexToDimensionPixelSize( tv.data, this.getResources().getDisplayMetrics()); }Copy the code

(3) View layout area height acquisition:

Rect = new Rect(); Rect = new Rect(); getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect); OnWindowFocusChanged is called when onResume and onPause are executed. The interface has been rendering end @ Override public void onWindowFocusChanged (Boolean hasFocus) {super. OnWindowFocusChanged (hasFocus); if (hasFocus) { int width = view.getMeasuredWidth(); Int height = view.getMeasuredheight (); View.post (new Runnable() {@override public void run() {int width=view.getMeasuredWidth(); int height=view.getMeasuredHeight(); }}) / / a fourth way the getViewTreeObserver () addOnGlobalLayoutListener (new ViewTreeObserver. OnGlobalLayoutListener () { @Override public void onGlobalLayout() { view.getViewTreeObserver().removeOnGlobalLayoutListener(this); int width=view.getMeasuredWidth(); int height=view.getMeasuredHeight(); }});Copy the code

(4) Height acquisition of App area:

 Rect rect = new Rect();
 getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
Copy the code

(5) Height acquisition of screen area:

// The first method, which has become obsolete since version 4.1. Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); // Display defaultDisplay = getWindowManager().getDefaultdisplay (); Point point = new Point(); defaultDisplay.getSize(point); int x = point.x; int y = point.y; Rect outSize = new Rect(); getWindowManager().getDefaultDisplay().getRectSize(outSize); int left = outSize.left; int top = outSize.top; int right = outSize.right; int bottom = outSize.bottom; DisplayMetrics outMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(outMetrics); int widthPixels = outMetrics.widthPixels; int heightPixels = outMetrics.heightPixels; Point outSizeOther = new Point(); getWindowManager().getDefaultDisplay().getRealSize(outSizeOther); int x1 = outSizeOther.x; int y1 = outSizeOther.y; DisplayMetrics outMetrics1 = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getRealMetrics(outMetrics1); int widthPixel = outMetrics1.widthPixels; int heightPixel = outMetrics1.heightPixels;Copy the code

Note in particular: these methods are best called after or after the Activity’s onWindowFocusChanged() method, because that’s when the display is truly complete.

Iii. Classification of Android coordinate system

Above we analyze the division of The Android screen area, and then we analyze the Android coordinate system related to the area. In the Android coordinate system, it can be divided into screen coordinate system and view coordinate system.

(1) Screen coordinate system

We have already introduced the difference between the screen coordinate system and the mathematical coordinate system. Taking the upper left corner of the screen as the origin of the coordinate, x axis to the right is the positive direction, and Y axis to the downward is the positive direction, as shown in the figure:

(2) View coordinate system

View coordinate system In the process of View drawing, drawing content will take the coordinate system as a reference, and finally determine the position of drawing content in View.

Let’s take a look at the usual coordinate methods, noting that they are relative to the parent container:

View related methods: Method description

View.getleft () : The distance between the left edge of the current view and the left edge of its parent view (view coordinates);

View.getright () : The distance between the right edge of the current view and the left edge of its parent view (view coordinates);

View.gettop () : The distance between the top edge of the current view and the top edge (top) of its parent view (view coordinates);

View.getbottom () : The distance between the bottom edge of the current view and the top edge (top) of its parent view (view coordinates);

View.gettranslationx () : the X-axis offset of the current View. The initial value is 0, the left offset value is negative, and the right offset value is positive; (Common in property animations)

View.gettranslationy () : the Y offset of the current View. Initial value is 0, upward offset is negative, downward offset is positive; (Common in property animations)

View.getX: the X axis offset of the current View. The initial value is 0, the left offset value is negative, and the right offset value is positive; The return value is getLeft()+getTranslationX(), which changes when setTranslationX() changes to getLeft().

View.getY: indicates the offset of the current View on the Y axis. Initial value is 0, upward offset is negative, downward offset is positive; The return value is getTop()+getTranslationY(). When setTranslationY() changes getTop(), getY() changes.

To illustrate these methods, a graph is prepared, as shown below:

At this point, we can obtain the View width and height method:

GetWidth () : width of the current View, i.e. getRight()-getLeft() getHeight() : width of the current View, i.e. getBottom()-getTop()

It is important to note that the above method should be used after the View measurement is completed, that is, after rendering is completed, otherwise the value will be 0.

Let’s take a look at some of the methods MotionEvent provides when a finger touches the screen:

GetX () : the distance between the center point and the left edge of the View (relative coordinates) getY() : the distance between the center point and the upper edge of the View (relative coordinates) getRawX() : Distance between touch center point and left edge of screen (absolute coordinates) getRawY() : Distance between touch center point and upper edge of screen (absolute coordinates)

To illustrate these methods, a graph is prepared, as shown below:

This is the end of today’s content, this article is mainly to describe the common methods in View and coordinate related concepts, but also for the later content to pave the way. Welcome to pay attention to the public account [Dragon Xuan] can get the latest updates oh.