/
TagTextDelegate.java
428 lines (388 loc) · 15 KB
/
TagTextDelegate.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package com.zpf.views.tagtext;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.text.TextPaint;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
public class TagTextDelegate {
private int lineHeight;
private int maxLines;
private int maxHeight;
private float paragraphSpace;
private float fontSize;
private String ellipsisText;
public final TagTextStyle defStyle = new TagTextStyle();
public final TagTextStyle ellipsisStyle = new TagTextStyle();
private final TagTextMeasureMan measureMan = new TagTextMeasureMan();
private int calculateHeight = -1;
private int showHeight = 0;
private int lastWidth = -1;
private final ArrayList<TagTextItem> contentTextList = new ArrayList<>();
private final TagTextPieceInfo ellipsisPart = new TagTextPieceInfo();
private final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
private final TagTextRecycler recycler = new TagTextRecycler();
private float upX = -1f;
private float upY = -1f;
private float downX = -1f;
private float downY = -1f;
private float lastY = -1f;
private View.OnClickListener defClickListener;
private TagItemClickListener itemClickListener;
private final View.OnClickListener clickDispatcher = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (System.currentTimeMillis() - downTime > 1000) {
return;
}
if (itemClickListener == null) {
if (defClickListener != null) {
defClickListener.onClick(v);
}
return;
}
if (ellipsisPart.startIndex < ellipsisPart.endIndex) {
if (ellipsisPart.left <= upX && ellipsisPart.right >= upX && ellipsisPart.top <= upY && ellipsisPart.bottom >= upY) {
itemClickListener.onClickEllipsis();
return;
}
}
boolean handled = false;
for (TagTextItem item : contentTextList) {
for (TagTextPieceInfo info : item.parts) {
if (info.left <= upX && info.right >= upX && info.top <= upY && info.bottom >= upY) {
handled = itemClickListener.onClickItem(item.textId);
if (!handled && defClickListener != null) {
defClickListener.onClick(v);
}
handled = true;
break;
}
}
if (handled) {
break;
}
}
if (!handled && defClickListener != null) {
defClickListener.onClick(v);
}
}
};
public void initDefConfig(float density) {
textPaint.density = density;
if (fontSize == 0f) {
fontSize = 14 * density;
}
if (lineHeight == 0) {
lineHeight = (int) (16 * density + 0.5f);
}
if (defStyle.color == 0) {
defStyle.color = Color.DKGRAY;
}
if (ellipsisStyle.color == 0) {
ellipsisStyle.color = Color.BLUE;
}
ellipsisStyle.bold = defStyle.bold;
ellipsisStyle.italic = defStyle.italic;
ellipsisStyle.underline = defStyle.underline;
ellipsisStyle.strikeThru = defStyle.strikeThru;
if (ellipsisText == null) {
ellipsisText = "...";
}
}
public View.OnClickListener getRealClickListener(View.OnClickListener l) {
defClickListener = l;
boolean isClickable = itemClickListener != null || defClickListener != null;
if (isClickable) {
return clickDispatcher;
} else {
return null;
}
}
public View.OnClickListener getRealClickListener(TagItemClickListener l) {
itemClickListener = l;
boolean isClickable = itemClickListener != null || defClickListener != null;
if (isClickable) {
return clickDispatcher;
} else {
return null;
}
}
private long downTime = 0L;
public boolean handleTouchEvent(@NonNull View drawOn, MotionEvent event) {
if (drawOn.isClickable() || drawOn.isLongClickable() || showHeight < calculateHeight) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
downX = event.getRawX();
downY = event.getRawY();
lastY = downY;
downTime = System.currentTimeMillis();
drawOn.cancelLongPress();
return true;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (showHeight < calculateHeight) {
int dY = (int) (event.getRawY() - lastY);
if (dY != 0) {
drawOn.scrollBy(0, dY);
}
}
lastY = event.getRawY();
if (Math.abs(event.getRawX() - downX) > 6 || Math.abs(event.getRawY() - downY) > 6) {
downTime = 0L;
drawOn.cancelLongPress();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
upX = event.getX();
upY = event.getY();
} else {
downTime = 0L;
drawOn.cancelLongPress();
}
return true;
}
return false;
}
public void onDraw(@NonNull View drawOn, Canvas canvas) {
if (canvas != null) {
canvas.translate(0, drawOn.getScrollY());
for (TagTextItem item : contentTextList) {
item.style.setPaintStyle(textPaint, defStyle.color);
if (item.parts.size() == 0) {
continue;
}
for (TagTextPieceInfo info : item.parts) {
if (info.shouldDraw()) {
canvas.drawText(item.textStr, info.startIndex, info.endIndex, info.drawX, info.drawY, textPaint);
} else {
break;
}
}
}
if (ellipsisPart.shouldDraw()) {
ellipsisStyle.setPaintStyle(textPaint, defStyle.color);
canvas.drawText(ellipsisText, ellipsisPart.startIndex, ellipsisPart.endIndex, ellipsisPart.drawX, ellipsisPart.drawY, textPaint);
}
}
}
public int checkBorder(int y) {
if (y > 0) {
y = 0;
} else if (y < showHeight - calculateHeight) {
y = showHeight - calculateHeight;
}
return y;
}
public int measureHeight(View drawOn, int widthMeasureSpec, int heightMeasureSpec) {
int currentWidth = View.MeasureSpec.getSize(widthMeasureSpec);
if (lastWidth != currentWidth) {
lastWidth = currentWidth;
calculateHeight = calculateDrawHeight(drawOn, View.MeasureSpec.getSize(widthMeasureSpec));
}
if (View.MeasureSpec.getMode(heightMeasureSpec) == View.MeasureSpec.EXACTLY) {
showHeight = View.MeasureSpec.getSize(heightMeasureSpec);
} else {
showHeight = calculateHeight;
}
if (maxHeight > 0 && showHeight > maxHeight) {
showHeight = maxHeight;
}
return View.MeasureSpec.makeMeasureSpec(showHeight, View.MeasureSpec.EXACTLY);
}
public TagTextStyle setContentText(String text, int id) {
contentTextList.clear();
TagTextItem contentItem = recycler.obtainTextItem(id, text, defStyle);
contentTextList.add(contentItem);
return contentItem.style;
}
public TagTextStyle getDefStyle() {
return defStyle;
}
public TagTextStyle addTextItem(String text, int id) {
return addTextItem(text, id, -1);
}
public TagTextStyle addTextItem(String text, int id, int index) {
TagTextItem item = recycler.obtainTextItem(id, text, defStyle);
if (index < 0 || index > contentTextList.size()) {
contentTextList.add(item);
} else {
contentTextList.add(index, item);
}
return item.style;
}
public void clearContent() {
contentTextList.clear();
}
public void setEllipsisText(String text) {
if (checkUnequals(text, ellipsisText)) {
ellipsisText = text;
}
}
public TagTextStyle getEllipsisStyle() {
return ellipsisStyle;
}
public void setLineHeight(int lineHeight) {
this.lineHeight = lineHeight;
}
public int getLineHeight() {
return lineHeight;
}
public int getMaxHeight() {
return maxHeight;
}
public void setMaxHeight(int maxHeight) {
this.maxHeight = maxHeight;
}
public int getLastCalculateHeight() {
return calculateHeight;
}
public void setMaxLines(int maxLines) {
this.maxLines = maxLines;
}
public int getMaxLines() {
return maxLines;
}
public void setFontSize(float fontSize) {
this.fontSize = fontSize;
}
public float getFontSize() {
return fontSize;
}
public void setParagraphSpace(float paragraphSpace) {
this.paragraphSpace = paragraphSpace;
}
public float getParagraphSpace() {
return paragraphSpace;
}
public void checkRefresh(View drawOn) {
if (lastWidth > 0 && drawOn != null) {
int newCalculateHeight = calculateDrawHeight(drawOn, drawOn.getWidth());
if (newCalculateHeight != calculateHeight) {
calculateHeight = newCalculateHeight;
drawOn.requestLayout();
} else {
drawOn.invalidate();
}
}
}
public boolean shouldDrawEllipsis() {
return ellipsisPart.shouldDraw();
}
public boolean canScrollVertically(int scrollY, int direction) {
if (direction == 0) {
return false;
}
if (direction < 0) {
return scrollY > 0;
} else {
return calculateHeight > showHeight + scrollY;
}
}
public boolean canScrollHorizontally(int scrollX, int direction) {
return false;
}
private boolean checkUnequals(CharSequence a, CharSequence b) {
if (a == null) {
return b != null && b.length() > 0;
}
if (b == null) {
return a.length() > 0;
}
return !a.equals(b);
}
public PointF getTextEndPoint() {
if (contentTextList.size() == 0) {
return new PointF(0f, 0f);
}
List<TagTextPieceInfo> lastTextParts = contentTextList.get(contentTextList.size() - 1).parts;
if (lastTextParts.size() == 0) {
return new PointF(0f, 0f);
}
TagTextPieceInfo lastPieceInfo = lastTextParts.get(lastTextParts.size() - 1);
return new PointF(lastPieceInfo.right, lastPieceInfo.bottom);
}
private int calculateDrawHeight(@NonNull View drawOn, int width) {
int realWidth = width - drawOn.getPaddingStart() - drawOn.getPaddingEnd();
if (realWidth <= 0 || contentTextList.size() == 0) {
return drawOn.getPaddingTop() + drawOn.getPaddingBottom();
}
textPaint.setTextSize(fontSize);
Paint.FontMetrics metrics = textPaint.getFontMetrics();
final float fontHeight = -metrics.ascent - metrics.descent;
final int lHeight = Math.max((int) (fontHeight + 0.5f), lineHeight);
float ellipsisWidth;
ellipsisPart.reset();
if (ellipsisText == null || ellipsisText.length() == 0) {
ellipsisWidth = 0;
} else {
ellipsisWidth = textPaint.measureText(ellipsisText);
}
recycler.reset(drawOn.getPaddingStart(), drawOn.getPaddingTop(), fontHeight, lHeight);
int currentLine = 1;
int startIndex;
float usedWidth = 0f;
boolean newline = false;
boolean newParagraph = false;
float addParagraphSpace = 0f;
TagTextItem item;
for (int i = 0; i < contentTextList.size(); i++) {
if (usedWidth < 0) {
break;
}
item = contentTextList.get(i);
item.parts.clear();
if (item.textStr == null || item.textStr.length() == 0) {
continue;
}
startIndex = 0;
TagTextMeasureMan.TagTextMeasureResult measureResult;
TagTextPieceInfo pieceInfo;
while (startIndex < item.textStr.length()) {
if (newline) {
currentLine++;
usedWidth = 0f;
if (newParagraph) {
addParagraphSpace = addParagraphSpace + paragraphSpace;
}
}
measureResult = measureMan.calculateDrawWidth(realWidth - usedWidth, item.textStr, textPaint, startIndex);
if (currentLine == maxLines) {
if (ellipsisWidth > 0) {
if (measureResult.newline || usedWidth + measureResult.drawWidth + ellipsisWidth >= realWidth) {
measureResult = measureMan.calculateDrawWidth(realWidth - usedWidth - ellipsisWidth,
item.textStr, textPaint, startIndex);
recycler.recombination(ellipsisPart, 0, ellipsisText.length(),
drawOn.getPaddingStart() + usedWidth + measureResult.drawWidth,
ellipsisWidth, currentLine, addParagraphSpace);
// recycler.recombination(ellipsisPart, 0, ellipsisText.length(),
// width - drawOn.getPaddingEnd() - ellipsisWidth, ellipsisWidth, currentLine, addParagraphSpace);
usedWidth = -1;
}
}
if (usedWidth > 0 && measureResult.newline) {
usedWidth = -1;
}
}
if (measureResult.drawWidth > 0) {
pieceInfo = recycler.obtainOnePiece(startIndex, measureResult.endIndex,
drawOn.getPaddingStart() + usedWidth, measureResult.drawWidth, currentLine, addParagraphSpace);
} else {
pieceInfo = null;
}
if (pieceInfo != null) {
item.parts.add(pieceInfo);
}
if (usedWidth < 0) {
break;
}
startIndex = measureResult.endIndex;
newline = measureResult.newline;
usedWidth = usedWidth + measureResult.drawWidth;
newParagraph = measureResult.newParagraph;
}
}
return (int) (drawOn.getPaddingTop() + drawOn.getPaddingBottom() + addParagraphSpace + 0.499f + currentLine * lHeight);
}
}