-
Notifications
You must be signed in to change notification settings - Fork 2
/
DummyView.java
81 lines (66 loc) · 2.38 KB
/
DummyView.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
package com.avinash.uihelper;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class DummyView extends View {
private List<View> views = new ArrayList<>();
private Drawable divider;
private int dividerWidth;
private int dividerHeight;
public DummyView(Context context) {
super(context);
}
public void setDivider(Drawable divider, int dividerWidth, int dividerHeight) {
if (divider != null) {
this.divider = divider;
this.dividerWidth = dividerWidth;
this.dividerHeight = dividerHeight;
divider.setBounds(0, 0, dividerWidth, dividerHeight);
}
}
/**
* Add a view for the DummyView to draw.
*
* @param childView View to draw
*/
public void addFakeView(View childView) {
childView.layout(0, 0, getWidth(), childView.getMeasuredHeight());
views.add(childView);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
final int len = views.size();
for (int i = 0; i < len; i++) {
View v = views.get(i);
v.layout(left, top, left + v.getMeasuredWidth(), top + v.getMeasuredHeight());
}
}
public void clearViews() {
views.clear();
}
@Override
public void dispatchDraw(Canvas canvas) {
canvas.save();
if (divider != null) {
divider.setBounds(0, 0, dividerWidth, dividerHeight);
}
final int len = views.size();
for (int i = 0; i < len; i++) {
View v = views.get(i);
canvas.save();
canvas.clipRect(0, 0, getWidth(), v.getMeasuredHeight());
v.draw(canvas);
canvas.restore();
if (divider != null) {
divider.draw(canvas);
canvas.translate(0, dividerHeight);
}
canvas.translate(0, v.getMeasuredHeight());
}
canvas.restore();
}
}