From 7402e364000fd053b7ee08650a04a8559977ba41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E6=AD=87=E5=B0=94?= Date: Sun, 10 Jan 2021 16:54:25 +0430 Subject: [PATCH] Added new methods (features) Added 6 new very useful methods. 1. getScreenXDpi() which returns the Width (X) density in DPI. 2. getScreenYDpi() which returns the Height (Y) density in DPI. 3. calculateDistanceByX() which returns the distance between the given View's X (start point of View's width) and the screen width. 4. calculateDistanceByY() which returns the distance between the given View's Y (start point of View's height) and the screen height. 5. getViewX() which returns the X coordinate of the given View on the screen. 6. getViewY() which returns the Y coordinate of the given View on the screen. --- .../com/blankj/utilcode/util/ScreenUtils.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/lib/utilcode/src/main/java/com/blankj/utilcode/util/ScreenUtils.java b/lib/utilcode/src/main/java/com/blankj/utilcode/util/ScreenUtils.java index b5cad7ef99..cb742b7a7d 100644 --- a/lib/utilcode/src/main/java/com/blankj/utilcode/util/ScreenUtils.java +++ b/lib/utilcode/src/main/java/com/blankj/utilcode/util/ScreenUtils.java @@ -112,6 +112,74 @@ public static float getScreenDensity() { public static int getScreenDensityDpi() { return Resources.getSystem().getDisplayMetrics().densityDpi; } + + + + + /** + * Return X (width) of the screen expressed as dots-per-inch. + * + * @return the width of screen density expressed as dots-per-inch + */ + public static int getScreenXDpi() { + return Resources.getSystem().getDisplayMetrics().xdpi; + } + + /** + * Return Y (height) of the screen expressed as dots-per-inch. + * + * @return the height of screen density expressed as dots-per-inch + */ + public static int getScreenYDpi() { + return Resources.getSystem().getDisplayMetrics().ydpi; + } + + + + /** + * Return the distance between the given View's X (start point of View's width) and the screen width. + * + * @return the distance between the given View's X (start point of View's width) and the screen width. + */ + public float calculateDistanceByX(View view) { + int[] point = new int[0]; + view.getLocationOnScreen(point); + return (getScreenWidth() - point[0]).toFloat(); + } + + /** + * Return the distance between the given View's Y (start point of View's height) and the screen height. + * + * @return the distance between the given View's Y (start point of View's height) and the screen height. + */ + public float calculateDistanceByY(View view) { + int[] point = new int[0]; + view.getLocationOnScreen(point); + return (getScreenHeight() - point[1]).toFloat(); + } + + /** + * Return the X coordinate of the given View on the screen. + * + * @return X coordinate of the given View on the screen. + */ + public int getViewX(View view){ + int[] point = new int[0]; + view.getLocationOnScreen(point); + return point[0]; + } + + /** + * Return the Y coordinate of the given View on the screen. + * + * @return Y coordinate of the given View on the screen. + */ + public int getViewY(View view){ + int[] point = new int[0]; + view.getLocationOnScreen(point); + return point[1]; + } + /** * Set full screen.