-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathAwesomeTextView.java
More file actions
296 lines (246 loc) · 10.4 KB
/
AwesomeTextView.java
File metadata and controls
296 lines (246 loc) · 10.4 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
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
package com.beardedhen.androidbootstrap;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.TextView;
import com.beardedhen.androidbootstrap.api.attributes.BootstrapBrand;
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.androidbootstrap.api.view.BootstrapBrandView;
import com.beardedhen.androidbootstrap.api.view.BootstrapTextView;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.beardedhen.androidbootstrap.font.IconSet;
import com.beardedhen.androidbootstrap.font.MaterialIcons;
import com.beardedhen.androidbootstrap.font.Typicon;
import java.io.Serializable;
/**
* This class extends the default Android TextView to supply Bootstrap behaviour. The text color
* can be set by changing the BootstrapBrand, and scalable Typeface icons can be interspersed with
* regular text, using the BootstrapText spannable.
*/
public class AwesomeTextView extends TextView implements BootstrapTextView, BootstrapBrandView {
private static final String TAG = "com.beardedhen.androidbootstrap.AwesomeTextView";
private BootstrapText bootstrapText;
private BootstrapBrand bootstrapBrand;
public enum AnimationSpeed {
FAST(500, 200),
MEDIUM(1000, 500),
SLOW(2000, 1000);
private final long rotateDuration;
private final long flashDuration;
AnimationSpeed(long rotateDuration, long flashDuration) {
this.rotateDuration = rotateDuration;
this.flashDuration = flashDuration;
}
public long getRotateDuration() {
return rotateDuration;
}
public long getFlashDuration() {
return flashDuration;
}
}
public AwesomeTextView(Context context) {
super(context);
initialise(null);
}
public AwesomeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initialise(attrs);
}
public AwesomeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialise(attrs);
}
private void initialise(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.AwesomeTextView);
String markdownText;
try {
int typeOrdinal = a.getInt(R.styleable.AwesomeTextView_bootstrapBrand, -1);
int faIconOrdinal = a.getInt(R.styleable.AwesomeTextView_fontAwesomeIcon, -1);
int typiconOrdinal = a.getInt(R.styleable.AwesomeTextView_typicon, -1);
int materialIconOrdinal = a.getInt(R.styleable.AwesomeTextView_materialIcon, -1);
boolean clickable = a.getBoolean(R.styleable.AwesomeTextView_android_clickable, true);
this.bootstrapBrand = DefaultBootstrapBrand.fromAttributeValue(typeOrdinal);
boolean editMode = isInEditMode();
if (typiconOrdinal != -1) {
final IconSet typicon = TypefaceProvider.retrieveRegisteredIconSet(Typicon.FONT_PATH, editMode);
if (!editMode) {
setIcon(typicon.iconCodeForAttrIndex(typiconOrdinal), typicon);
}
}
if (faIconOrdinal != -1) {
final IconSet fontAwesome = TypefaceProvider.retrieveRegisteredIconSet(FontAwesome.FONT_PATH, editMode);
if (!editMode) {
setIcon(fontAwesome.iconCodeForAttrIndex(faIconOrdinal), fontAwesome);
}
}
if (materialIconOrdinal != -1) {
final IconSet materialIcons = TypefaceProvider.retrieveRegisteredIconSet(MaterialIcons.FONT_PATH, editMode);
if (!editMode) {
setIcon(materialIcons.iconCodeForAttrIndex(materialIconOrdinal), materialIcons);
}
}
markdownText = a.getString(R.styleable.AwesomeTextView_bootstrapText);
setClickable(clickable); // allows view to reach android:state_pressed
int gravity = a.getInt(R.styleable.AwesomeTextView_android_gravity, Gravity.CENTER);
setGravity(gravity);
}
finally {
a.recycle();
}
if (markdownText != null) {
setMarkdownText(markdownText);
}
updateBootstrapState();
}
@Override public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable(TAG, super.onSaveInstanceState());
bundle.putSerializable(BootstrapTextView.KEY, bootstrapText);
bundle.putSerializable(BootstrapBrand.KEY, bootstrapBrand);
return bundle;
}
@Override public void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
Serializable text = bundle.getSerializable(BootstrapTextView.KEY);
Serializable brand = bundle.getSerializable(BootstrapBrand.KEY);
if (brand instanceof BootstrapBrand) {
bootstrapBrand = (BootstrapBrand) brand;
}
if (text instanceof BootstrapText) {
bootstrapText = (BootstrapText) text;
}
state = bundle.getParcelable(TAG);
}
super.onRestoreInstanceState(state);
updateBootstrapState();
}
/**
* Starts a Flashing Animation on the AwesomeTextView
*
* @param forever whether the animation should be infinite or play once
* @param speed how fast the item should flash
*/
public void startFlashing(boolean forever, AnimationSpeed speed) {
Animation fadeIn = new AlphaAnimation(0, 1);
//set up extra variables
fadeIn.setDuration(50);
fadeIn.setRepeatMode(Animation.REVERSE);
//default repeat count is 0, however if user wants, set it up to be infinite
fadeIn.setRepeatCount(0);
if (forever) {
fadeIn.setRepeatCount(Animation.INFINITE);
}
fadeIn.setStartOffset(speed.getFlashDuration());
startAnimation(fadeIn);
}
/**
* Starts a rotating animation on the AwesomeTextView
*
* @param clockwise true for clockwise, false for anti clockwise spinning
* @param speed how fast the item should rotate
*/
public void startRotate(boolean clockwise, AnimationSpeed speed) {
Animation rotate;
//set up the rotation animation
if (clockwise) {
rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
}
else {
rotate = new RotateAnimation(360, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
}
//set up some extra variables
rotate.setRepeatCount(Animation.INFINITE);
rotate.setInterpolator(new LinearInterpolator());
rotate.setStartOffset(0);
rotate.setRepeatMode(Animation.RESTART);
rotate.setDuration(speed.getRotateDuration());
startAnimation(rotate);
}
/**
* Sets the text to display a FontIcon, replacing whatever text is already present.
* Used to set the text to display a FontAwesome Icon.
*
* @param iconSet - An implementation of FontIcon
*/
public void setIcon(CharSequence iconCode, IconSet iconSet) {
setBootstrapText(new BootstrapText.Builder(getContext(), isInEditMode()).addIcon(iconCode, iconSet).build());
}
/**
* Sets the text to display a FontIcon, replacing whatever text is already present.
* Used to set the text to display a FontAwesome Icon.
*
* @param iconCode the fontawesome icon code e.g. "fa_play"
*/
public void setFontAwesomeIcon(@FontAwesome.Icon CharSequence iconCode) {
setBootstrapText(new BootstrapText.Builder(getContext(), isInEditMode()).addFontAwesomeIcon(iconCode).build());
}
/**
* Sets the text to display a MaterialIcon, replacing whatever text is already present.
* Used to set the text to display a MaterialIcon Icon.
*
* @param iconCode the fontawesome icon code e.g. "md_share"
*/
public void setMaterialIcon(@FontAwesome.Icon CharSequence iconCode) {
setBootstrapText(new BootstrapText.Builder(getContext(), isInEditMode()).addMaterialIcon(iconCode).build());
}
/**
* Sets the text to display a FontIcon, replacing whatever text is already present.
* Used to set the text to display a Typicon.
*
* @param iconCode the typicon icon code e.g. "ty_adjust_brightness"
*/
public void setTypicon(@Typicon.Icon CharSequence iconCode) {
setBootstrapText(new BootstrapText.Builder(getContext(), isInEditMode()).addTypicon(iconCode).build());
}
@Override public void setMarkdownText(String text) {
setBootstrapText(IconResolver.resolveMarkdown(getContext(), text, isInEditMode()));
}
protected void updateBootstrapState() {
if (bootstrapText != null) {
setText(bootstrapText);
}
if (bootstrapBrand != null) {
setTextColor(bootstrapBrand.defaultFill(getContext()));
}
}
/*
* Getters/Setters
*/
@Override public void setBootstrapText(BootstrapText bootstrapText) {
this.bootstrapText = bootstrapText;
updateBootstrapState();
}
@Nullable @Override public BootstrapText getBootstrapText() {
return bootstrapText;
}
@Override public void setBootstrapBrand(@NonNull BootstrapBrand bootstrapBrand) {
this.bootstrapBrand = bootstrapBrand;
updateBootstrapState();
}
@NonNull @Override public BootstrapBrand getBootstrapBrand() {
return bootstrapBrand;
}
@Override public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
bootstrapText = null;
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
if(!(text!=null && text.length()>0)){
setVisibility(GONE);
}else{
setVisibility(VISIBLE);
}
}
}