| 14792 |
manas |
1 |
/**
|
|
|
2 |
* Copyright 2010-present Facebook.
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
package com.facebook.internal;
|
|
|
18 |
|
|
|
19 |
import android.content.Context;
|
|
|
20 |
import android.graphics.*;
|
|
|
21 |
import android.util.TypedValue;
|
|
|
22 |
import android.view.Gravity;
|
|
|
23 |
import android.view.ViewGroup;
|
|
|
24 |
import android.widget.FrameLayout;
|
|
|
25 |
import android.widget.TextView;
|
|
|
26 |
import com.facebook.android.R;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* com.facebook.internal is solely for the use of other packages within the Facebook SDK for Android. Use of
|
|
|
30 |
* any of the classes in this package is unsupported, and they may be modified or removed without warning at
|
|
|
31 |
* any time.
|
|
|
32 |
*/
|
|
|
33 |
public class LikeBoxCountView extends FrameLayout {
|
|
|
34 |
|
|
|
35 |
public enum LikeBoxCountViewCaretPosition {
|
|
|
36 |
LEFT,
|
|
|
37 |
TOP,
|
|
|
38 |
RIGHT,
|
|
|
39 |
BOTTOM
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
private TextView likeCountLabel;
|
|
|
43 |
private LikeBoxCountViewCaretPosition caretPosition = LikeBoxCountViewCaretPosition.LEFT;
|
|
|
44 |
|
|
|
45 |
private float caretHeight;
|
|
|
46 |
private float caretWidth;
|
|
|
47 |
private float borderRadius;
|
|
|
48 |
private Paint borderPaint;
|
|
|
49 |
private int textPadding;
|
|
|
50 |
private int additionalTextPadding;
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Constructor
|
|
|
54 |
*
|
|
|
55 |
* @param context Context for this View
|
|
|
56 |
*/
|
|
|
57 |
public LikeBoxCountView(Context context) {
|
|
|
58 |
super(context);
|
|
|
59 |
initialize(context);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Sets the text for this view
|
|
|
64 |
* @param text
|
|
|
65 |
*/
|
|
|
66 |
public void setText(String text) {
|
|
|
67 |
likeCountLabel.setText(text);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Sets the caret's position. This will trigger a layout of the view.
|
|
|
72 |
* @param caretPosition
|
|
|
73 |
*/
|
|
|
74 |
public void setCaretPosition(LikeBoxCountViewCaretPosition caretPosition) {
|
|
|
75 |
this.caretPosition = caretPosition;
|
|
|
76 |
|
|
|
77 |
// Since the presence of a caret will move that edge closer to the text, let's add
|
|
|
78 |
// some padding (equal to caretHeight) in that same direction
|
|
|
79 |
switch (caretPosition) {
|
|
|
80 |
case LEFT:
|
|
|
81 |
setAdditionalTextPadding(additionalTextPadding, 0, 0, 0);
|
|
|
82 |
break;
|
|
|
83 |
case TOP:
|
|
|
84 |
setAdditionalTextPadding(0, additionalTextPadding, 0, 0);
|
|
|
85 |
break;
|
|
|
86 |
case RIGHT:
|
|
|
87 |
setAdditionalTextPadding(0, 0, additionalTextPadding, 0);
|
|
|
88 |
break;
|
|
|
89 |
case BOTTOM:
|
|
|
90 |
setAdditionalTextPadding(0, 0, 0, additionalTextPadding);
|
|
|
91 |
break;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
@Override
|
|
|
97 |
protected void onDraw(Canvas canvas) {
|
|
|
98 |
super.onDraw(canvas);
|
|
|
99 |
|
|
|
100 |
int top = getPaddingTop(), left = getPaddingLeft();
|
|
|
101 |
int right = getWidth() - getPaddingRight(), bottom = getHeight() - getPaddingBottom();
|
|
|
102 |
|
|
|
103 |
switch (caretPosition) {
|
|
|
104 |
case BOTTOM:
|
|
|
105 |
bottom -= caretHeight;
|
|
|
106 |
break;
|
|
|
107 |
case LEFT:
|
|
|
108 |
left += caretHeight;
|
|
|
109 |
break;
|
|
|
110 |
case TOP:
|
|
|
111 |
top += caretHeight;
|
|
|
112 |
break;
|
|
|
113 |
case RIGHT:
|
|
|
114 |
right -= caretHeight;
|
|
|
115 |
break;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
drawBorder(canvas, left, top, right, bottom);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
private void initialize(Context context) {
|
|
|
122 |
setWillNotDraw(false); // Required for the onDraw() method to be called on a FrameLayout
|
|
|
123 |
caretHeight = getResources().getDimension(R.dimen.com_facebook_likeboxcountview_caret_height);
|
|
|
124 |
caretWidth = getResources().getDimension(R.dimen.com_facebook_likeboxcountview_caret_width);
|
|
|
125 |
borderRadius = getResources().getDimension(R.dimen.com_facebook_likeboxcountview_border_radius);
|
|
|
126 |
|
|
|
127 |
borderPaint = new Paint();
|
|
|
128 |
borderPaint.setColor(getResources().getColor(R.color.com_facebook_likeboxcountview_border_color));
|
|
|
129 |
borderPaint.setStrokeWidth(getResources().getDimension(R.dimen.com_facebook_likeboxcountview_border_width));
|
|
|
130 |
borderPaint.setStyle(Paint.Style.STROKE);
|
|
|
131 |
|
|
|
132 |
initializeLikeCountLabel(context);
|
|
|
133 |
|
|
|
134 |
addView(likeCountLabel);
|
|
|
135 |
|
|
|
136 |
setCaretPosition(this.caretPosition);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
private void initializeLikeCountLabel(Context context) {
|
|
|
140 |
likeCountLabel = new TextView(context);
|
|
|
141 |
LayoutParams likeCountLabelLayoutParams = new LayoutParams(
|
|
|
142 |
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
143 |
ViewGroup.LayoutParams.MATCH_PARENT);
|
|
|
144 |
likeCountLabel.setLayoutParams(likeCountLabelLayoutParams);
|
|
|
145 |
likeCountLabel.setGravity(Gravity.CENTER);
|
|
|
146 |
likeCountLabel.setTextSize(
|
|
|
147 |
TypedValue.COMPLEX_UNIT_PX,
|
|
|
148 |
getResources().getDimension(R.dimen.com_facebook_likeboxcountview_text_size));
|
|
|
149 |
likeCountLabel.setTextColor(getResources().getColor(R.color.com_facebook_likeboxcountview_text_color));
|
|
|
150 |
textPadding = getResources().getDimensionPixelSize(R.dimen.com_facebook_likeboxcountview_text_padding);
|
|
|
151 |
|
|
|
152 |
// Calculate the additional text padding that will be applied in the direction of the caret.
|
|
|
153 |
additionalTextPadding = getResources().getDimensionPixelSize(R.dimen.com_facebook_likeboxcountview_caret_height);
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
private void setAdditionalTextPadding(int left, int top, int right, int bottom) {
|
|
|
157 |
likeCountLabel.setPadding(
|
|
|
158 |
textPadding + left,
|
|
|
159 |
textPadding + top,
|
|
|
160 |
textPadding + right,
|
|
|
161 |
textPadding + bottom);
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
private void drawBorder(Canvas canvas, float left, float top, float right, float bottom) {
|
|
|
165 |
Path borderPath = new Path();
|
|
|
166 |
|
|
|
167 |
float ovalSize = 2.0f * borderRadius;
|
|
|
168 |
|
|
|
169 |
// Top left corner
|
|
|
170 |
borderPath.addArc(new RectF(left, top, left + ovalSize, top + ovalSize), -180, 90);
|
|
|
171 |
|
|
|
172 |
// Top caret
|
|
|
173 |
if (caretPosition == LikeBoxCountViewCaretPosition.TOP) {
|
|
|
174 |
borderPath.lineTo(left + (right - left - caretWidth) / 2, top);
|
|
|
175 |
borderPath.lineTo(left + (right - left) / 2, top - caretHeight);
|
|
|
176 |
borderPath.lineTo(left + (right - left + caretWidth) / 2, top);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
// Move to top right corner
|
|
|
180 |
borderPath.lineTo(right - borderRadius, top);
|
|
|
181 |
|
|
|
182 |
// Top right corner
|
|
|
183 |
borderPath.addArc(new RectF(right - ovalSize, top, right, top + ovalSize), -90, 90);
|
|
|
184 |
|
|
|
185 |
// Right caret
|
|
|
186 |
if (caretPosition == LikeBoxCountViewCaretPosition.RIGHT) {
|
|
|
187 |
borderPath.lineTo(right, top + (bottom - top - caretWidth) / 2);
|
|
|
188 |
borderPath.lineTo(right + caretHeight, top + (bottom - top) / 2);
|
|
|
189 |
borderPath.lineTo(right, top + (bottom - top + caretWidth) / 2);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
// Move to bottom right corner
|
|
|
193 |
borderPath.lineTo(right, bottom - borderRadius);
|
|
|
194 |
|
|
|
195 |
// Bottom right corner
|
|
|
196 |
borderPath.addArc(new RectF(right - ovalSize, bottom - ovalSize, right, bottom), 0, 90);
|
|
|
197 |
|
|
|
198 |
// Bottom caret
|
|
|
199 |
if (caretPosition == LikeBoxCountViewCaretPosition.BOTTOM) {
|
|
|
200 |
borderPath.lineTo(left + (right - left + caretWidth) / 2, bottom);
|
|
|
201 |
borderPath.lineTo(left + (right - left) / 2, bottom + caretHeight);
|
|
|
202 |
borderPath.lineTo(left + (right - left - caretWidth) / 2, bottom);
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
// Move to bottom left corner
|
|
|
206 |
borderPath.lineTo(left + borderRadius, bottom);
|
|
|
207 |
|
|
|
208 |
// Bottom left corner
|
|
|
209 |
borderPath.addArc(new RectF(left, bottom - ovalSize, left + ovalSize, bottom), 90, 90);
|
|
|
210 |
|
|
|
211 |
// Left caret
|
|
|
212 |
if (caretPosition == LikeBoxCountViewCaretPosition.LEFT) {
|
|
|
213 |
borderPath.lineTo(left, top + (bottom - top + caretWidth) / 2);
|
|
|
214 |
borderPath.lineTo(left - caretHeight, top + (bottom - top) / 2);
|
|
|
215 |
borderPath.lineTo(left, top + (bottom - top - caretWidth) / 2);
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
// Move back to the beginning
|
|
|
219 |
borderPath.lineTo(left, top + borderRadius);
|
|
|
220 |
|
|
|
221 |
canvas.drawPath(borderPath, borderPaint);
|
|
|
222 |
}
|
|
|
223 |
}
|