aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/FontAwesomeText.java
blob: 75a130c5ac0e76c397b787f357e0fadcae8b62de (plain)
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
package com.beardedhen.androidbootstrap;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.R;

public class FontAwesomeText extends FrameLayout {

	private static Typeface font;
	private static Map<String, String> faMap;

	private TextView tv;
	
	private static final String FA_ICON_QUESTION = "fa-question";
	
	public enum AnimationSpeed
	{
		FAST,
		MEDIUM, 
		SLOW;
	}
	
	static{
		faMap = FontAwesome.getFaMap();
	}
	
	public FontAwesomeText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initialise(attrs);
	}

	public FontAwesomeText(Context context, AttributeSet attrs) {
		super(context, attrs);
		initialise(attrs);
	}

	public FontAwesomeText(Context context) {
		super(context);
		initialise(null);
	}


	
	
	private void initialise( AttributeSet attrs )
	{
		LayoutInflater inflator = (LayoutInflater)getContext().getSystemService(
			    Context.LAYOUT_INFLATER_SERVICE);

		//get font
		readFont(getContext());

		TypedArray a = getContext().obtainStyledAttributes(attrs,  R.styleable.FontAwesomeText);

		//inflate the view
		View fontAwesomeTextView = inflator.inflate(R.layout.font_awesome_text, null, false);
		tv = (TextView)fontAwesomeTextView.findViewById(R.id.lblText);
		
		String icon = "";
		float fontSize = 14.0f;
		
		//icon
		if (a.getString(R.styleable.FontAwesomeText_fa_icon) != null) {
			icon = a.getString(R.styleable.FontAwesomeText_fa_icon);
		}
		
		//font size
		if (a.getString(R.styleable.FontAwesomeText_android_textSize) != null) {

			String xmlProvidedSize = attrs.getAttributeValue(
					"http://schemas.android.com/apk/res/android", "textSize"); 
			final Pattern PATTERN_FONT_SIZE = Pattern
					.compile("([0-9]+[.]?[0-9]*)sp"); 
			Matcher m = PATTERN_FONT_SIZE.matcher(xmlProvidedSize);

			if (m.find()) { 
				if (m.groupCount() == 1) { 
					fontSize = Float.valueOf(m.group(1));
				} 
			} 
		}
		
		//text colour
		if(a.getString(R.styleable.FontAwesomeText_android_textColor) != null){
			tv.setTextColor(a.getColor(R.styleable.FontAwesomeText_android_textColor, R.color.bbutton_inverse));
		}
		
		setIcon(icon);
		
		tv.setTypeface(font);
		tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
		
		a.recycle();
		addView(fontAwesomeTextView);
	}

	private static void readFont(Context context)
	{
		
		if(font == null){	
			try {
			font = Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf");
			} catch (Exception e) {
                Log.e("BButton", "Could not get typeface because " + e.getMessage());
                font = Typeface.DEFAULT;
            }
		}

	}
	
	
	/**
	 * Used to start flashing a FontAwesomeText item
	 * @param context the current applications context
	 * @param forever whether the item should flash repeatedly or just once
	 * @param speed how fast the item should flash, chose between FontAwesomeText.AnimationSpeed.SLOW / 
	 * FontAwesomeText.AnimationSpeed.MEDIUM / FontAwesomeText.AnimationSpeed.FAST 
	 */
	public void startFlashing(Context context, 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);
	    }
	    
	    //default speed
	    fadeIn.setStartOffset(1000);
	    
	    //fast
	    if (speed.equals(AnimationSpeed.FAST))
	    {
	    	fadeIn.setStartOffset(200);
	    }
	    
	    //medium
	    if (speed.equals(AnimationSpeed.MEDIUM))
	    {
	    	fadeIn.setStartOffset(500);
	    }

	    //set the new animation to a final animation
	    final Animation animation = fadeIn;
		
	  //run the animation - used to work correctly on older devices
		tv.postDelayed(new Runnable() {
			@Override
			public void run() {
				tv.startAnimation(animation);
			}
		}, 100);
	}
	
	
	/**
	 * Used to start rotating a FontAwesomeText item
	 * @param context the current applications context
	 * @param clockwise true for clockwise, false for anti clockwise spinning
	 * @param speed how fast the item should flash, chose between FontAwesomeText.AnimationSpeed.SLOW / 
	 * FontAwesomeText.AnimationSpeed.MEDIUM / FontAwesomeText.AnimationSpeed.FAST 
	 */
	public void startRotate(Context context, 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);
		
		//defaults
		rotate.setDuration(2000);
		
		//fast
	    if (speed.equals(AnimationSpeed.FAST))
	    {
	    	rotate.setDuration(500);
	    }
	    
	    //medium
	    if (speed.equals(AnimationSpeed.MEDIUM))
	    {
	    	rotate.setDuration(1000);
	    }
		
	    //send the new animation to a final animation
		final Animation animation = rotate;
		
		//run the animation - used to work correctly on older devices
		tv.postDelayed(new Runnable() {
			@Override
			public void run() {
				tv.startAnimation(animation);
			}
		}, 100);

	}
	
	
	/**
	 * Used to stop animating any FontAwesomeText item
	 */
	public void stopAnimation(){
		//stop the animation
		tv.clearAnimation();
	}
	
	
	/**
	 * Used to set the icon for a FontAwesomeText item
	 * @param faIcon - String value for the icon as per http://fortawesome.github.io/Font-Awesome/cheatsheet/
	 */
	public void setIcon(String faIcon) {
		
		String icon = faMap.get(faIcon);
		
		if (icon == null)
		{
			icon = faMap.get(FA_ICON_QUESTION);
		}
		
		tv.setText(icon);
	}
	
	/**
	 * Used to set the text color of the underlying text view.
	 * @param color - Integer value representing a color resource.
	 */
	public void setTextColor(int color) {
		tv.setTextColor(color);
	}

	/**
	 * Used to set the text size of the underlying text view.
	 * @param unit - Integer value representing a unit size
	 * @param size - Float value representing text size
	 */
	public void setTextSize(int unit, float size) {
		tv.setTextSize(unit, size);
	}
	
}