BadgeView.java
5.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
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
181
182
183
184
185
186
187
188
package com.cnlive.goldenline.ui.widget;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TabWidget;
import android.widget.TextView;
public class BadgeView extends TextView {
private boolean mHideOnNull = true;
public BadgeView(Context context) {
this(context, null);
}
public BadgeView(Context context, AttributeSet attrs) {
this(context, attrs, 16842884);
}
public BadgeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
if (!(getLayoutParams() instanceof FrameLayout.LayoutParams)) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
-2,
-2,
53);
setLayoutParams(layoutParams);
}
setTextColor(-1);
setTypeface(Typeface.DEFAULT_BOLD);
setTextSize(2, 11.0F);
setPadding(dip2Px(5.0F), dip2Px(1.0F), dip2Px(5.0F), dip2Px(1.0F));
setBackground(9, Color.parseColor("#d3321b"));
setGravity(17);
setHideOnNull(true);
setBadgeCount(0);
}
public void setBackground(int dipRadius, int badgeColor) {
int radius = dip2Px(dipRadius);
float[] radiusArray = {radius, radius, radius, radius,
radius, radius, radius, radius};
RoundRectShape roundRect = new RoundRectShape(radiusArray, null, null);
ShapeDrawable bgDrawable = new ShapeDrawable(roundRect);
bgDrawable.getPaint().setColor(badgeColor);
setBackgroundDrawable(bgDrawable);
}
public boolean isHideOnNull() {
return this.mHideOnNull;
}
public void setHideOnNull(boolean hideOnNull) {
this.mHideOnNull = hideOnNull;
setText(getText());
}
public void setText(CharSequence text, TextView.BufferType type) {
if ((isHideOnNull()) && (
(text == null) || (text.toString().equalsIgnoreCase("0"))))
setVisibility(8);
else {
setVisibility(0);
}
super.setText(text, type);
}
public void setBadgeCount(int count) {
setText(String.valueOf(count));
}
public Integer getBadgeCount() {
if (getText() == null) {
return null;
}
String text = getText().toString();
try {
return Integer.valueOf(Integer.parseInt(text));
} catch (NumberFormatException e) {
}
return null;
}
public void setBadgeGravity(int gravity) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
params.gravity = gravity;
setLayoutParams(params);
}
public int getBadgeGravity() {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
return params.gravity;
}
public void setBadgeMargin(int dipMargin) {
setBadgeMargin(dipMargin, dipMargin, dipMargin, dipMargin);
}
public void setBadgeMargin(int leftDipMargin, int topDipMargin, int rightDipMargin, int bottomDipMargin) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
params.leftMargin = dip2Px(leftDipMargin);
params.topMargin = dip2Px(topDipMargin);
params.rightMargin = dip2Px(rightDipMargin);
params.bottomMargin = dip2Px(bottomDipMargin);
setLayoutParams(params);
}
public int[] getBadgeMargin() {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
return new int[]{params.leftMargin, params.topMargin,
params.rightMargin, params.bottomMargin};
}
public void incrementBadgeCount(int increment) {
Integer count = getBadgeCount();
if (count == null)
setBadgeCount(increment);
else
setBadgeCount(increment + count.intValue());
}
public void decrementBadgeCount(int decrement) {
incrementBadgeCount(-decrement);
}
public void setTargetView(TabWidget target, int tabIndex) {
View tabView = target.getChildTabViewAt(tabIndex);
setTargetView(tabView);
}
public void setTargetView(View target) {
if (getParent() != null) {
((ViewGroup) getParent()).removeView(this);
}
if (target == null) {
return;
}
if ((target.getParent() instanceof FrameLayout)) {
((FrameLayout) target.getParent()).addView(this);
} else if ((target.getParent() instanceof ViewGroup)) {
ViewGroup parentContainer = (ViewGroup) target.getParent();
int groupIndex = parentContainer.indexOfChild(target);
parentContainer.removeView(target);
FrameLayout badgeContainer = new FrameLayout(getContext());
ViewGroup.LayoutParams parentlayoutParams = target
.getLayoutParams();
badgeContainer.setLayoutParams(parentlayoutParams);
target.setLayoutParams(new ViewGroup.LayoutParams(
-1,
-1));
parentContainer.addView(badgeContainer, groupIndex,
parentlayoutParams);
badgeContainer.addView(target);
badgeContainer.addView(this);
} else if (target.getParent() == null) {
Log.e(getClass().getSimpleName(), "ParentView is needed");
}
}
private int dip2Px(float dip) {
return (int) (dip *
getContext().getResources().getDisplayMetrics().density + 0.5F);
}
}