-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayouts.java
More file actions
74 lines (63 loc) · 2.26 KB
/
Layouts.java
File metadata and controls
74 lines (63 loc) · 2.26 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.nosorogstudio.views;
import android.view.View;
public class Layouts {
public static class Anchor {
public static final int CENTER_HORIZONTAL = 0x01;
public static final int LEFT = 0x03;
public static final int RIGHT = 0x05;
public static final int CENTER_VERTICAL = 0x10;
public static final int TOP = 0x30;
public static final int BOTTOM = 0x50;
public static final int BASELINE = 0x90;
public static final int CENTER = 0x11;
public static float getX(View view, int anchor) {
switch (anchor & 0x0f) {
case CENTER_HORIZONTAL:
return 0.5f;
case LEFT:
return 0.0f;
case RIGHT:
return 1.0f;
default:
return 0.5f;
}
}
public static float getY(View view, int anchor) {
switch (anchor & 0xf0) {
case CENTER_VERTICAL:
return 0.5f;
case TOP:
return 0.0f;
case BOTTOM:
return 1.0f;
case BASELINE: {
int baseline = view.getBaseline();
if (baseline < 0) {
return 1.0f;
}
int height = view.getMeasuredHeight();
return (float) baseline / (float) height;
}
default:
return 0.5f;
}
}
}
public static void layoutChildAt(View child, int x, int y, int anchor) {
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
float anchorX = Layouts.Anchor.getX(child, anchor);
float anchorY = Layouts.Anchor.getY(child, anchor);
int offsetX = Math.round(childWidth * anchorX);
int offsetY = Math.round(childHeight * anchorY);
child.layout(
x - offsetX,
y - offsetY,
x - offsetX + childWidth,
y - offsetY + childHeight
);
}
public static void layoutChildCenterAt(View child, int x, int y) {
layoutChildAt(child, x, y, Anchor.CENTER);
}
}