Util.java
1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.cnlive.strike.xiaojiaspeaker;
import android.content.res.Resources;
import android.graphics.RectF;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
/**
* utils
*/
final class Util {
public static RectF calculateRectOnScreen(View view) {
int[] location = new int[2];
view.getLocationOnScreen(location);
return new RectF(location[0], location[1], location[0] + view.getMeasuredWidth(), location[1] + view.getMeasuredHeight());
}
public static RectF calculateRectInWindow(View view) {
int[] location = new int[2];
view.getLocationInWindow(location);
return new RectF(location[0], location[1], location[0] + view.getMeasuredWidth(), location[1] + view.getMeasuredHeight());
}
public static float pxToDp(float px) {
return px / Resources.getSystem().getDisplayMetrics().density;
}
public static float dpToPx(float dp) {
return dp * Resources.getSystem().getDisplayMetrics().density;
}
public static int gravityToArrowDirection(int gravity) {
switch (gravity) {
case Gravity.START:
return Gravity.END;
case Gravity.TOP:
return Gravity.BOTTOM;
case Gravity.END:
return Gravity.START;
case Gravity.BOTTOM:
return Gravity.TOP;
default:
return gravity;
}
}
public static void removeOnGlobalLayoutListener(View view, ViewTreeObserver.OnGlobalLayoutListener listener) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
}
}
}