在Android中使用自定义字体正确 [英] Using Custom Fonts Properly in Android

查看:161
本文介绍了在Android中使用自定义字体正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经延长的TextView 使用自定义的字体(如描述<一个href=\"http://stackoverflow.com/questions/14631326/use-roboto-font-in-app-with-minimum-api-level-14/14633032#14633032\">here),即,

So I've extended TextView to use a custom font (as described here), i.e.,

public class CustomTextView extends TextView {
    public static final int CUSTOM_TEXT_NORMAL = 1;
    public static final int CUSTOM_TEXT_BOLD = 2;

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initCustomTextView(context, attrs);
    }

    private void initCustomTextView(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView, 0, 0);
        int typeface = array.getInt(R.styleable.CustomTextView_typeface, CUSTOM_TEXT_NORMAL);
        array.recycle();
        setCustomTypeface(typeface);
    }

    public setCustomTypeface(int typeface) {
         switch(typeface) {
             case CUSTOM_TEXT_NORMAL:
                 Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "customTextNormal.ttf");
                 setTypeface(tf);
                 break;
             case CUSTOM_TEXT_BOLD: 
                 Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "customTextBold.ttf");
                 setTypeface(tf); 
                 break;
         }
     } 
}

我再加入到活动的主要布局片段使用 CustomTextView 。所有工作正常,但似乎有一些内存的问题,也就是说,我每次旋转屏幕时间(导致活动要经过其生命周期),字体资产装入机堆除了previous加载。例如;下面是从亚行外壳dumpsys meminfo的my.package.com 屏幕转储初始加载后没有屏幕旋转(使用的Roboto-Light字体):

I then use CustomTextView in a fragment added to activity's main layout. All works fine, but there appears to be some memory issues, i.e., every time I rotate the screen (causing the activity to go through its life cycle), the font assets are loaded into the native heap in addition to the previous load. For example; below is a screen dump from adb shell dumpsys meminfo my.package.com after the initial load and no screen rotations (using Roboto-Light font):

和几rotaions后的相同屏幕转储

and the same screen dump after a few rotaions

什么是明确的是在增加的资产配置的和的的每个屏幕旋转时本机堆的(GC没有任何明确这件事)。当然,那么我们不应该使用自定义字体在上面描述的方式,如果没有,我们应该如何使用自定义字体?

What is clear is the increase in the Asset Allocations and the Native Heap that occurs on each screen rotation (GC doesn't clear this up either). Surely then we shouldn't be using custom fonts in the manner described above and, if not, how should we be using custom fonts?

推荐答案

您应该找到答案<一个href=\"https://stackoverflow.com/questions/16901930/memory-leaks-with-custom-font-for-set-custom-font\">here.

基本上,你需要建立自己的系统在创建之后他们缓存的字体(因此你只能使用一次创建每个字体)。

Basically you need to build your own system to cache those typefaces after you create them (and therefore you'll only be creating each typeface once).

这篇关于在Android中使用自定义字体正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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