Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 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 Answer

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());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...