Androidの代替アプリフォント--Type face
テーマに戻ると、アプリのフォントを切り替える構想は一般的にカスタムコントロール(TextView、EditView)を思い浮かべますが、オンライン上の項目にどれだけのコントロールがあるのでしょうか.Stuido置換ツールで置き換えてもリスクやリソースの消費を負担します.主にこの考え方が堅苦しいので、Androidの下部でフォント設定を開放する方法があるべきだと考えています.その後、Applicationでコントロールをフィルタリングして置き換えることができ、検索するとやはり発見され、次のコードが貼られます.
:
1、 Application
FontUtils.getInstance().replaceSystemDefaultFontFromAsset(this, "fonts/xxx.ttf");
2、
monospace
package core.util; import android.app.Application; import android.content.Context; import android.graphics.Typeface; import android.support.annotation.NonNull; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.lang.ref.SoftReference; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class FontUtils { private static final String TAG = FontUtils.class.getSimpleName(); private Map
> mCache = new HashMap<>(); private static FontUtils sSingleton = null; public static Typeface DEFAULT = Typeface.DEFAULT; // disable instantiate private FontUtils() {} public static FontUtils getInstance() { // double check if (sSingleton == null) { synchronized(FontUtils.class) { if (sSingleton == null) { sSingleton = new FontUtils(); } } } return sSingleton; } /** * Replace the font of specified view and it's children
* @param root The root view. * @param fontPath font file path relative to 'assets' directory. */ public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath) { replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath)); } /** *Replace the font of specified view and it's children
* @param root The root view. * @param fontPath font file path relative to 'assets' directory. * @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC} */ public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath, int style) { replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath), style); } /** *Replace the font of specified view and it's children
* @param root The root view. * @param fontPath The full path to the font data. */ public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath) { replaceFont(root, createTypefaceFromFile(fontPath)); } /** *Replace the font of specified view and it's children
* @param root The root view. * @param fontPath The full path to the font data. * @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC} */ public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath, int style) { replaceFont(root, createTypefaceFromFile(fontPath), style); } /** *Replace the font of specified view and it's children with specified typeface
*/ private void replaceFont(@NonNull View root, @NonNull Typeface typeface) { if (root == null || typeface == null) { return; } if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font TextView textView = (TextView)root; // Extract previous style of TextView int style = Typeface.NORMAL; if (textView.getTypeface() != null) { style = textView.getTypeface().getStyle(); } textView.setTypeface(typeface, style); } else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views ViewGroup viewGroup = (ViewGroup) root; for (int i = 0; i < viewGroup.getChildCount(); ++i) { replaceFont(viewGroup.getChildAt(i), typeface); } } // else return } /** *Replace the font of specified view and it's children with specified typeface and text style
* @param style One of {@link Typeface#NORMAL}, {@link Typeface#BOLD}, {@link Typeface#ITALIC}, {@link Typeface#BOLD_ITALIC} */ private void replaceFont(@NonNull View root, @NonNull Typeface typeface, int style) { if (root == null || typeface == null) { return; } if (style < 0 || style > 3) { style = Typeface.NORMAL; } if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font TextView textView = (TextView)root; textView.setTypeface(typeface, style); } else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views ViewGroup viewGroup = (ViewGroup) root; for (int i = 0; i < viewGroup.getChildCount(); ++i) { replaceFont(viewGroup.getChildAt(i), typeface, style); } } // else return } /** *Create a Typeface instance with specified font file
* @param fontPath font file path relative to 'assets' directory. * @return Return created typeface instance. */ private Typeface createTypefaceFromAsset(Context context, String fontPath) { SoftReferencetypefaceRef = mCache.get(fontPath); Typeface typeface = null; if (typefaceRef == null || (typeface = typefaceRef.get()) == null) { typeface = Typeface.createFromAsset(context.getAssets(), fontPath); typefaceRef = new SoftReference<>(typeface); mCache.put(fontPath, typefaceRef); } return typeface; } private Typeface createTypefaceFromFile(String fontPath) { SoftReference typefaceRef = mCache.get(fontPath); Typeface typeface = null; if (typefaceRef == null || (typeface = typefaceRef.get()) == null) { typeface = Typeface.createFromFile(fontPath); typefaceRef = new SoftReference<>(typeface); mCache.put(fontPath, typefaceRef); } return typeface; } /** * Replace system default font. Note:you should also add code below to your app theme in styles.xml.
* {@code- monospace
} *The best place to call this method is {@link Application#onCreate()}, it will affect * whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.
* @param context {@link Context Context} * @param fontPath font file path relative to 'assets' directory. */ public void replaceSystemDefaultFontFromAsset(@NonNull Context context, @NonNull String fontPath) { replaceSystemDefaultFont(createTypefaceFromAsset(context, fontPath)); } /** *Replace system default font. Note:you should also add code below to your app theme in styles.xml.
* {@code- monospace
} *The best place to call this method is {@link Application#onCreate()}, it will affect * whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.
* @param context {@link Context Context} * @param fontPath The full path to the font data. */ public void replaceSystemDefaultFontFromFile(@NonNull Context context, @NonNull String fontPath) { replaceSystemDefaultFont(createTypefaceFromFile(fontPath)); } /** *Replace system default font. Note:you should also add code below to your app theme in styles.xml.
* {@code- monospace
} *The best place to call this method is {@link Application#onCreate()}, it will affect * whole app font.If you call this method after view is visible, you need to invalid the view to make it effective.
*/ private void replaceSystemDefaultFont(@NonNull Typeface typeface) { modifyObjectField(null, "MONOSPACE", typeface); } private void modifyObjectField(Object obj, String fieldName, Object value) { try { Field defaultField = Typeface.class.getDeclaredField(fieldName); defaultField.setAccessible(true); defaultField.set(obj, value); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
コアコードは:replaceFontメソッドで、TextViewのフォントを置き換えると、このツールクラスはTextviewのフォントだけを置き換えているのではないかと疑問になりますが、EditView、RadioButtonなどを使っているとします.コントロールの親を見てみると、TextViewを継承しているので、おおらかで、細部が成否を決めるのではないでしょうか.ツールクラス全体がフォント置換の効率性に現れ、ソフトリファレンスとHashMapキャッシュポリシーを採用して置換時のリソース消費を大幅に低減し、確かに全面的に考慮し、反射メカニズムを採用してType-faceを設定してフォント交換の目的を達成した.
このツールクラスはほとんど完璧で、確かに多くの学習に値するところがあります.例えば、単例の設定ではsynchronizedを採用して怠け者のモードを捨て、リソースの使用にSoftReferenceソフトリファレンスを使用し、キャッシュにHashMapを使用し、最後に反射賦値を採用しています.これらの点はすべて輪可点です.キャッシュされたHashMapをConcurrentHashMapに変更すると、マルチスレッド環境でパフォーマンスが向上する可能性があります.
以下に、ツールクラスの作成者GitHubを示します.https://github.com/whinc