-
Notifications
You must be signed in to change notification settings - Fork 3
/
ExcelLayout.java
180 lines (147 loc) · 7.12 KB
/
ExcelLayout.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.vinnie.weight;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
* 表格布局
*
* 注意:第一个子View的高度决定了所有行的高度。
*
* @author Vinnie
*/
public class ExcelLayout extends ViewGroup {
final static String TAG = "ExcelLayout";
int maxSpanCountX = 0;
int maxSpanCountY = 0;
float maxItemSizeX = 0f;
int maxItemSizeY = 0;
int borderColor;
Paint borderPaint;
public ExcelLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ExcelLayout);
borderColor = array.getColor(R.styleable.ExcelLayout_ELBorderColor, Color.BLACK);
array.recycle();
init();
}
private void init() {
borderPaint = new Paint();
borderPaint.setStyle(Paint.Style.FILL);
borderPaint.setColor(borderColor);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
int width = 0;
int height = 0;
int cCount = getChildCount();
int childWidthMeasureSpec = getChildMeasureSpec(MeasureSpec.UNSPECIFIED, 0, LayoutParams.WRAP_CONTENT);
int childHeightMeasureSpec = getChildMeasureSpec(MeasureSpec.UNSPECIFIED, 0, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < cCount; i++) {
View childFirst = getChildAt(i);
if (i == 0) {
measureChild(childFirst, childWidthMeasureSpec, childHeightMeasureSpec);
maxItemSizeY = Math.max(maxItemSizeY, childFirst.getMeasuredHeight());
}
ExcelLayoutParam paramFirst = (ExcelLayoutParam) childFirst.getLayoutParams();
maxSpanCountX = Math.max(maxSpanCountX, paramFirst.startX + paramFirst.spanX);
maxSpanCountY = Math.max(maxSpanCountY, paramFirst.startY + paramFirst.spanY);
}
height = maxSpanCountY * maxItemSizeY;
if (modeWidth == MeasureSpec.EXACTLY) {
maxItemSizeX = 1f * sizeWidth / maxSpanCountX;
}
for (int i = 0; i < cCount; i++) {
View child = getChildAt(i);
ExcelLayoutParam param = (ExcelLayoutParam) child.getLayoutParams();
childWidthMeasureSpec = getChildMeasureSpec(MeasureSpec.EXACTLY, 0, Math.round(param.spanX * maxItemSizeX));
childHeightMeasureSpec = getChildMeasureSpec(MeasureSpec.EXACTLY, 0, param.spanY * maxItemSizeY);
measureChild(child, childWidthMeasureSpec, childHeightMeasureSpec);
}
setMeasuredDimension(
modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width,
modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height
);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int cCount = getChildCount();
for (int i=0; i<cCount; i++) {
View child = getChildAt(i);
ExcelLayoutParam param = (ExcelLayoutParam) child.getLayoutParams();
child.layout(Math.round(param.startX * maxItemSizeX), param.startY * maxItemSizeY, Math.round((param.startX + param.spanX) * maxItemSizeX), (param.startY + param.spanY) * maxItemSizeY);
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
float paintHalfWidth = 0.5f;
borderPaint.setStrokeWidth(paintHalfWidth * 2);
int cCount = getChildCount();
for (int i = 0; i < cCount; i++) {
View child = getChildAt(i);
ExcelLayoutParam param = (ExcelLayoutParam) child.getLayoutParams();
if (param.startX == 0) {
canvas.drawLine(param.startX * maxItemSizeX + paintHalfWidth, param.startY * maxItemSizeY, (param.startX + param.spanX) * maxItemSizeX + paintHalfWidth, param.startY * maxItemSizeY, borderPaint);
} else {
canvas.drawLine(param.startX * maxItemSizeX, param.startY * maxItemSizeY, (param.startX + param.spanX) * maxItemSizeX, param.startY * maxItemSizeY, borderPaint);
}
if (param.startX + param.spanX >= maxSpanCountX) {
canvas.drawLine((param.startX + param.spanX) * maxItemSizeX - paintHalfWidth, param.startY * maxItemSizeY, (param.startX + param.spanX) * maxItemSizeX - paintHalfWidth, (param.startY + param.spanY) * maxItemSizeY, borderPaint);
} else {
canvas.drawLine((param.startX + param.spanX) * maxItemSizeX, param.startY * maxItemSizeY, (param.startX + param.spanX) * maxItemSizeX, (param.startY + param.spanY) * maxItemSizeY, borderPaint);
}
if (param.startY + param.spanY >= maxSpanCountY) {
canvas.drawLine((param.startX + param.spanX) * maxItemSizeX, (param.startY + param.spanY) * maxItemSizeY - paintHalfWidth, param.startX * maxItemSizeX, (param.startY + param.spanY) * maxItemSizeY - paintHalfWidth, borderPaint);
} else {
canvas.drawLine((param.startX + param.spanX) * maxItemSizeX, (param.startY + param.spanY) * maxItemSizeY, param.startX * maxItemSizeX, (param.startY + param.spanY) * maxItemSizeY, borderPaint);
}
canvas.drawLine(param.startX * maxItemSizeX, (param.startY + param.spanY) * maxItemSizeY, param.startX * maxItemSizeX, param.startY * maxItemSizeY, borderPaint);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
protected LayoutParams generateLayoutParams(LayoutParams p) {
return new ExcelLayoutParam(p);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new ExcelLayoutParam(getContext(), attrs);
}
public class ExcelLayoutParam extends LayoutParams {
int startX = 0;
int startY = 0;
int spanX = 0;
int spanY = 0;
public ExcelLayoutParam(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray array = c.obtainStyledAttributes(attrs, R.styleable.ExcelLayout);
startX = array.getInt(R.styleable.ExcelLayout_ELStartX, 0);
startY = array.getInt(R.styleable.ExcelLayout_ELStartY, 0);
spanX = array.getInt(R.styleable.ExcelLayout_ELSpanX, 1);
spanY = array.getInt(R.styleable.ExcelLayout_ELSpanY, 1);
array.recycle();
}
public ExcelLayoutParam(LayoutParams source) {
super(source);
if (source instanceof ExcelLayoutParam) {
ExcelLayoutParam e = (ExcelLayoutParam) source;
this.startX = e.startX;
this.startY = e.startY;
this.spanX = e.spanX;
this.spanY = e.spanY;
}
}
}
}