Skip to content

Instantly share code, notes, and snippets.

@lpellegr
Last active August 29, 2015 14:13
Show Gist options
  • Save lpellegr/0e895f43b6959fc232b8 to your computer and use it in GitHub Desktop.
Save lpellegr/0e895f43b6959fc232b8 to your computer and use it in GitHub Desktop.
Roboto
import android.content.Context;
import android.graphics.Typeface;
import android.os.Build;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.TextView;
import java.lang.ref.SoftReference;
import java.util.HashMap;
/**
* Manages roboto typeface assets.
* <p/>
* Font files can be downloaded from the following link:
* <p/>
* http://material-design.storage.googleapis.com/downloads/RobotoTTF.zip
* <p/>
* Files should be placed in src/main/assets
*
* @author Laurent Pellegrino
*/
public enum Roboto {
BLACK,
BLACK_ITALIC,
BOLD,
BOLD_ITALIC,
CONDENSED,
CONDENSED_BOLD,
CONDENSED_BOLD_ITALIC,
CONDENSED_ITALIC,
CONDENSED_LIGHT,
CONDENSED_LIGHT_ITALIC,
CONDENSED_REGULAR,
ITALIC,
LIGHT,
LIGHT_ITALIC,
MEDIUM,
MEDIUM_ITALIC,
REGULAR,
THIN,
THIN_ITALIC;
private static final Cache<String, Typeface> CACHE = new Cache<>(values().length);
/**
* Apply the current typeface to the text view identified by {@code textView} if found in the
* specified {@code container} and if the current Android version code is greater than or
* equals to the given [@code versionCode}.
*
* @param context Context used to locate asset files.
* @param container Container used to locate the specified textview.
* @param textView The resource id identifying the textview to style.
* @param versionCode Minimum Android version code required to apply the typeface.
*/
public void applyOn(Context context, View container, @IdRes int textView, int versionCode) {
if (Build.VERSION.SDK_INT < versionCode) {
applyOn(context, container, textView);
}
}
/**
* Apply the current typeface to the specified {@code textView} if the current Android version
* code is greater than or equals to the given [@code versionCode}.
*
* @param context Context used to locate asset files.
* @param textView The textview to style.
* @param versionCode Minimum Android version code required to apply the typeface.
*/
public void applyOnIfOlderThanJellyBean(Context context, TextView textView, int versionCode) {
if (Build.VERSION.SDK_INT < versionCode) {
applyOn(context, textView);
}
}
/**
* Apply the current typeface to the text view identified by {@code textView} if found in the
* specified {@code container}.
*
* @param context Context used to locate asset files.
* @param container Container used to locate the specified textview.
* @param textView The resource id identifying the textview to style.
*/
public void applyOn(Context context, View container, @IdRes int textView) {
View found = container.findViewById(textView);
if (found != null) {
applyOn(context, (TextView) found);
}
}
/**
* Apply the current typeface to the specified {@code textView}.
*
* @param context Context used to locate asset files.
* @param textView The textview to style.
*/
public void applyOn(Context context, TextView textView) {
String name = name();
Typeface found;
synchronized (CACHE) {
found = CACHE.get(name);
if (found == null) {
found = Typeface.createFromAsset(context.getAssets(), getAssociatedFilename());
CACHE.put(name, found);
}
}
textView.setTypeface(found);
}
private String getAssociatedFilename() {
String[] chunks = name().split("_");
StringBuilder result = new StringBuilder();
result.append("Roboto-");
for (String chunk : chunks) {
result.append(capitalize(chunk));
}
result.append(".ttf");
return result.toString();
}
private static String capitalize(String value) {
StringBuilder result = new StringBuilder();
result.append(value.charAt(0));
int len = value.length();
for (int i = 1; i < len; i++) {
result.append(Character.toLowerCase(value.charAt(i)));
}
return result.toString();
}
private static final class Cache<K, V> {
private final HashMap<K, SoftReference<V>> map;
public Cache(int capacity) {
map = new HashMap<>(capacity);
}
public void put(K key, V value) {
map.put(key, new SoftReference<V>(value));
}
public V get(K key) {
SoftReference<V> found = map.get(key);
if (found != null) {
return found.get();
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment