-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberTextView.java
More file actions
272 lines (246 loc) · 9.48 KB
/
NumberTextView.java
File metadata and controls
272 lines (246 loc) · 9.48 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
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
package com.USisFounderOfISIS.NumberTextView;
/*
* This is the source code of Telegram for Android v. 5.x.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2013-2018.
*/
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Locale;
public class NumberTextView extends View {
private ArrayList<StaticLayout> letters = new ArrayList<>();
private ArrayList<StaticLayout> oldLetters = new ArrayList<>();
private TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
private ObjectAnimator animator;
private float progress = 0.0f;
private int currentNumber = 0;
private boolean addNumber;
private boolean center;
private float textWidth;
private float oldTextWidth;
private final float density;
private OnTextWidthProgressChangedListener onTextWidthProgressChangedListener;
public NumberTextView(@NonNull Context context) {
this(context, (AttributeSet) null);
}
public NumberTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 16842884);
}
public NumberTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
//this(context, attrs, defStyleAttr, 0);
super(context, attrs, defStyleAttr);
density = context.getResources().getDisplayMetrics().density;
}
public void setOnTextWidthProgressChangedListener(OnTextWidthProgressChangedListener onTextWidthProgressChangedListener) {
this.onTextWidthProgressChangedListener = onTextWidthProgressChangedListener;
}
@Keep
public void setProgress(float value) {
if (progress == value) {
return;
}
progress = value;
if (onTextWidthProgressChangedListener != null) {
onTextWidthProgressChangedListener.onTextWidthProgress(oldTextWidth, textWidth, progress);
}
invalidate();
}
@Keep
public float getProgress() {
return progress;
}
public void setAddNumber() {
addNumber = true;
}
public void setNumber(int number, boolean animated, boolean formatted) {
if (currentNumber == number && animated) {
return;
}
if (animator != null) {
animator.cancel();
animator = null;
}
oldLetters.clear();
oldLetters.addAll(letters);
letters.clear();
String oldText;
String text;
boolean forwardAnimation;
if (addNumber) {
oldText = String.format(Locale.US, "#%d", currentNumber);
text = String.format(Locale.US, "#%d", number);
forwardAnimation = number < currentNumber;
} else {
oldText = String.format(Locale.US, "%d", currentNumber);
text = String.format(Locale.US, "%d", number);
forwardAnimation = number > currentNumber;
}
if(formatted){
text = String.format(Locale.US,"%,d\n", number);
oldText = String.format(Locale.US,"%,d\n", currentNumber);
}
boolean replace = false;
textWidth = textPaint.measureText(text);
oldTextWidth = textPaint.measureText(oldText);
if (center) {
if (textWidth != oldTextWidth) {
replace = true;
}
}
currentNumber = number;
progress = 0;
for (int a = 0; a < text.length(); a++) {
String ch = text.substring(a, a + 1);
String oldCh = !oldLetters.isEmpty() && a < oldText.length() ? oldText.substring(a, a + 1) : null;
if (!replace && oldCh != null && oldCh.equals(ch)) {
letters.add(oldLetters.get(a));
oldLetters.set(a, null);
} else {
if (replace && oldCh == null) {
oldLetters.add(new StaticLayout("", textPaint, 0, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false));
}
StaticLayout layout = new StaticLayout(ch, textPaint, (int) Math.ceil(textPaint.measureText(ch)), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
letters.add(layout);
}
}
if (animated && !oldLetters.isEmpty()) {
animator = ObjectAnimator.ofFloat(this, "progress", forwardAnimation ? -1 : 1, 0);
animator.setDuration(addNumber ? 180 : 150);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
animator = null;
oldLetters.clear();
}
});
animator.start();
} else if (onTextWidthProgressChangedListener != null) {
onTextWidthProgressChangedListener.onTextWidthProgress(oldTextWidth, textWidth, progress);
}
invalidate();
}
public void setTextSize(int size) {
textPaint.setTextSize(density*size);
textPaint.setTextSize(toSP(size));
oldLetters.clear();
letters.clear();
setNumber(currentNumber, false, false);
}
public void setTextColor(int value) {
textPaint.setColor(value);
invalidate();
}
public void setTypeface(Typeface typeface) {
textPaint.setTypeface(typeface);
oldLetters.clear();
letters.clear();
setNumber(currentNumber, false, false);
}
public void setCenterAlign(boolean center) {
this.center = center;
}
@Override
protected void onDraw(@NonNull Canvas canvas) {
if (letters.isEmpty()) {
return;
}
float height = letters.get(0).getHeight();
float translationHeight = addNumber ? density*4 : height;
float x = 0;
float oldDx = 0;
if (center) {
x = (getMeasuredWidth() - textWidth) / 2f;
oldDx = (getMeasuredWidth() - oldTextWidth) / 2f - x;
}
canvas.save();
canvas.translate(getPaddingLeft() + x, (getMeasuredHeight() - height) / 2);
int count = Math.max(letters.size(), oldLetters.size());
for (int a = 0; a < count; a++) {
canvas.save();
StaticLayout old = a < oldLetters.size() ? oldLetters.get(a) : null;
StaticLayout layout = a < letters.size() ? letters.get(a) : null;
if (progress > 0) {
if (old != null) {
textPaint.setAlpha((int) (255 * progress));
canvas.save();
canvas.translate(oldDx, (progress - 1.0f) * translationHeight);
old.draw(canvas);
canvas.restore();
if (layout != null) {
textPaint.setAlpha((int) (255 * (1.0f - progress)));
canvas.translate(0, progress * translationHeight);
}
} else {
textPaint.setAlpha(255);
}
} else if (progress < 0) {
if (old != null) {
textPaint.setAlpha((int) (255 * -progress));
canvas.save();
canvas.translate(oldDx, (1.0f + progress) * translationHeight);
old.draw(canvas);
canvas.restore();
}
if (layout != null) {
if (a == count - 1 || old != null) {
textPaint.setAlpha((int) (255 * (1.0f + progress)));
canvas.translate(0, progress * translationHeight);
} else {
textPaint.setAlpha(255);
}
}
} else if (layout != null) {
textPaint.setAlpha(255);
}
if (layout != null) {
layout.draw(canvas);
}
canvas.restore();
canvas.translate(layout != null ? layout.getLineWidth(0) : old.getLineWidth(0) + density*1, 0);
if (layout != null && old != null) {
oldDx += old.getLineWidth(0) - layout.getLineWidth(0);
}
}
canvas.restore();
}
public float getOldTextWidth() {
return oldTextWidth;
}
public float getTextWidth() {
return textWidth;
}
private int toDP(int size){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
size, getResources().getDisplayMetrics());
}
private int toSP(int size){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
size, getResources().getDisplayMetrics());
}
public interface OnTextWidthProgressChangedListener {
/**
* Notifies layout that text width has changed
* @param fromWidth Old text width value
* @param toWidth New text width value
* @param progress Progress for the animation
*/
void onTextWidthProgress(float fromWidth, float toWidth, float progress);
}
}