Skip to content

Commit 7402e36

Browse files
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.
1 parent 10ae070 commit 7402e36

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

lib/utilcode/src/main/java/com/blankj/utilcode/util/ScreenUtils.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,74 @@ public static float getScreenDensity() {
112112
public static int getScreenDensityDpi() {
113113
return Resources.getSystem().getDisplayMetrics().densityDpi;
114114
}
115+
116+
117+
118+
119+
/**
120+
* Return X (width) of the screen expressed as dots-per-inch.
121+
*
122+
* @return the width of screen density expressed as dots-per-inch
123+
*/
124+
public static int getScreenXDpi() {
125+
return Resources.getSystem().getDisplayMetrics().xdpi;
126+
}
127+
128+
/**
129+
* Return Y (height) of the screen expressed as dots-per-inch.
130+
*
131+
* @return the height of screen density expressed as dots-per-inch
132+
*/
133+
public static int getScreenYDpi() {
134+
return Resources.getSystem().getDisplayMetrics().ydpi;
135+
}
136+
137+
138+
139+
/**
140+
* Return the distance between the given View's X (start point of View's width) and the screen width.
141+
*
142+
* @return the distance between the given View's X (start point of View's width) and the screen width.
143+
*/
144+
public float calculateDistanceByX(View view) {
145+
int[] point = new int[0];
146+
view.getLocationOnScreen(point);
147+
return (getScreenWidth() - point[0]).toFloat();
148+
}
149+
150+
/**
151+
* Return the distance between the given View's Y (start point of View's height) and the screen height.
152+
*
153+
* @return the distance between the given View's Y (start point of View's height) and the screen height.
154+
*/
155+
public float calculateDistanceByY(View view) {
156+
int[] point = new int[0];
157+
view.getLocationOnScreen(point);
158+
return (getScreenHeight() - point[1]).toFloat();
159+
}
160+
161+
/**
162+
* Return the X coordinate of the given View on the screen.
163+
*
164+
* @return X coordinate of the given View on the screen.
165+
*/
166+
public int getViewX(View view){
167+
int[] point = new int[0];
168+
view.getLocationOnScreen(point);
169+
return point[0];
170+
}
171+
172+
/**
173+
* Return the Y coordinate of the given View on the screen.
174+
*
175+
* @return Y coordinate of the given View on the screen.
176+
*/
177+
public int getViewY(View view){
178+
int[] point = new int[0];
179+
view.getLocationOnScreen(point);
180+
return point[1];
181+
}
182+
115183

116184
/**
117185
* Set full screen.

0 commit comments

Comments
 (0)