制作字体助手类 [英] Making a TypeFace Helper Class

查看:98
本文介绍了制作字体助手类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约10-15 活动的或片段的在我的应用程序。我有我使用约5种不同的字体(主要是 Roboto 变种)。

在几乎所有I类必须这样做:

  roboto_light = Typeface.createFromAsset(getActivity()。getAssets()
        字体/ roboto_light.ttf);
roboto_thin = Typeface.createFromAsset(getActivity()。getAssets(),
        字体/ roboto_thin.ttf);
roboto_regular = Typeface.createFromAsset(getActivity()。getAssets(),
        字体/ roboto_regular.ttf);
 

不是所有的类使用所有五个。有些使用1,有的采用4,有的采用3,而其他人可能使用的不同的的组合3。

本声明code。在每个班级似乎是多余的。可在5字体的所有声明一次,也许当应用程序启动,然后我用了一个辅助类静态使用它们?

我不知道如果我要做到这一点 - 如果可能的话 - 在扩展应用程序,或者只是一个普通的类,我可以静态调用类?哪里会这样被初始化?

解决方案
  

我不知道如果我要做到这一点 - 如果可能的话 - 一类   扩展应用程序,或者只是一个普通的类,我可以   静态调用?

无论哪种方式都行。有几个示例实现在那里,这是所有高速缓存中创建的最后几个类型的面孔。如果我没有记错,在最近的Andr​​oid平台的缓存也发生在引擎盖下。不管怎么说,基本实现是这样的:

 公共类字样{

    私有静态最后的Hashtable<字符串,字体>缓存=新的Hashtable<字符串,字体>();

    公共静态字样的get(上下文C,字符串名称){
        同步(缓存){
            如果(!cache.containsKey(名)){
                字样T = Typeface.createFromAsset(c.getAssets()的String.Format(字体/%s.ttf,名称));
                cache.put(姓名,吨);
            }
            返回cache.get(名称);
        }
    }
}
 

来源:<一href="https://$c$c.google.com/p/android/issues/detail?id=9904#c3">https://$c$c.google.com/p/android/issues/detail?id=9904#c3

这是使用一个辅助类,但你也可以让它属于你自己的应用程序扩展。它创建型面懒惰地:它试图首先检索面从本地缓存的类型,以及唯一的实例化新的一个,如果不能从高速缓存中。只需提供一个上下文和面类加载名称。

I have about 10-15 Activity's or Fragment's in my app. I have about 5 different TypeFaces I am using (mostly Roboto variants).

In almost every Class I have to do this:

roboto_light = Typeface.createFromAsset(getActivity().getAssets(),
        "fonts/roboto_light.ttf");
roboto_thin = Typeface.createFromAsset(getActivity().getAssets(),
        "fonts/roboto_thin.ttf");
roboto_regular = Typeface.createFromAsset(getActivity().getAssets(),
        "fonts/roboto_regular.ttf"); 

Not all classes use all five. Some use 1, some use 4, some use 3, while others may use a different combo of 3.

Declaring this code in every class seems redundant. Can the 5 fonts all be declared once, maybe when app starts and then I use a Helper Class to statically use them?

I am not sure if I have to do this -- if possible at all -- in a class that extends Application, or just a regular Class that I can statically call? And where would this be initialized?

解决方案

I am not sure if I have to do this -- if possible at all -- in a class that extends Application, or just a regular Class that I can statically call?

Either way will do. There are a couple of sample implementations out there, which all 'cache' the last few type faces created. If I recall correctly, in more recent Android platforms caching also happens under the hood. Anyways, a basic implementation would look like this:

public class Typefaces{

    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface get(Context c, String name){
        synchronized(cache){
            if(!cache.containsKey(name)){
                Typeface t = Typeface.createFromAsset(c.getAssets(), String.format("fonts/%s.ttf", name));
                cache.put(name, t);
            }
            return cache.get(name);
        }
    }    
}

Source: https://code.google.com/p/android/issues/detail?id=9904#c3

This is using a helper class, but you could also make it part your own Application extension. It creates type faces lazily: it attempts to retrieve the type face from the local cache first, and only instantiates a new one if not available from cache. Simply supply a Context and the name of the type face to load.

这篇关于制作字体助手类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆