Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

android - Access a typeface once from asset and use it as a reference

I am trying to create a method that returns titleFont and contentFont typeface so that it would improve efficiency by just returning reference and reduce length of code. I know its easy but I am not getting the way. Can anyone help please. or any alternatives would be appreciated.. Sorry for bad english

Here is the method that will be running several times..

protected void onPostExecute(Article result) {
    super.onPostExecute(result);

    TextView txtTitle= (TextView) view.findViewById(R.id.title);
    txtTitle.setTypeface(titleFont);
    txtTitle.setText(result.getTitle());

    private Typeface titleFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateCondMonoLgt.ttf");
    private Typeface contentFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateLight.ttf");

    TextView txtMain= (TextView) view.findViewById(R.id.main);
    txtMain.setTypeface(contentFont);
    txtMain.setText(result.getContent());
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I hope this help you ;)

FontUtil class

public class FontUtil {

    private static Typeface mTitleFont;
    private static Typeface mContentFont;

    public static enum FontType {

        TITLE_FONT {
            public String toString() {
                return "fonts/InterstateCondMonoLgt.ttf";
            }
        },

        CONTENT_FONT {
            public String toString() {
                return "fonts/InterstateLight.ttf";
            }
        }
    }

    /**
     * @return Typeface Instance with the font passed as parameter
     */
    public static Typeface getTypeface(Context context, String typefaceName) {
        Typeface typeFace = null;

        try {
            if (typefaceName.equals(FontType.TITLE_FONT.toString())) {
                if (mTitleFont == null) {
                    mTitleFont = Typeface.createFromAsset(context.getAssets(), typefaceName);
                }
                typeFace = mTitleFont;
            } else if (typefaceName.equals(FontType.CONTENT_FONT.toString())) {
                if (mContentFont == null) {
                    mContentFont = Typeface.createFromAsset(context.getAssets(), typefaceName);
                }
                typeFace = mContentFont;
            } 
        } catch (Exception ex) {
            typeFace = Typeface.DEFAULT;
        }

        return typeFace;
    }

    /**
     * @return Typeface Instance with the font passed as parameter
     */
    public static Typeface getTypeface(Context context, FontType typefaceName) {
        return getTypeface(context, typefaceName.toString());
    }
}

Client

protected void onPostExecute(Article result) {
    super.onPostExecute(result);

    TextView txtTitle= (TextView) view.findViewById(R.id.title);
    txtTitle.setTypeface( FontUtil.getTypeface(mContext, FontType.TITLE_FONT) );
    txtTitle.setText(result.getTitle());

    TextView txtMain= (TextView) view.findViewById(R.id.main);
    txtMain.setTypeface( FontUtil.getTypeface(mContext, FontType.CONTENT_FONT) );
    txtMain.setText(result.getContent());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...