内存泄漏与自定义字体设置自定义字体(Memory leaks with custom font for set custom font)

编程入门 行业动态 更新时间:2024-10-22 08:05:57
内存泄漏与自定义字体设置自定义字体(Memory leaks with custom font for set custom font)

以下用于设置自定义字体的代码会降低我的整个应用程序的速度。 我如何修改它以避免内存泄漏并提高速度并管理内存?

public class FontTextView extends TextView { private static final String TAG = "FontTextView"; public FontTextView(Context context) { super(context); } public FontTextView(Context context, AttributeSet attrs) { super(context, attrs); setCustomFont(context, attrs); } public FontTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setCustomFont(context, attrs); } private void setCustomFont(Context ctx, AttributeSet attrs) { TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.FontTextView); String customFont = a.getString(R.styleable.FontTextView_customFont); setCustomFont(ctx, customFont); a.recycle(); } public boolean setCustomFont(Context ctx, String asset) { Typeface tf = null; try { tf = Typeface.createFromAsset(ctx.getAssets(),"fonts/"+ asset); } catch (Exception e) { Log.e(TAG, "Could not get typeface: "+e.getMessage()); return false; } setTypeface(tf); return true; } }

The following code for setting custom fonts slows down my whole app. how do i modify it to avoid memory leaks and increase the speed and manage memory well?

public class FontTextView extends TextView { private static final String TAG = "FontTextView"; public FontTextView(Context context) { super(context); } public FontTextView(Context context, AttributeSet attrs) { super(context, attrs); setCustomFont(context, attrs); } public FontTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setCustomFont(context, attrs); } private void setCustomFont(Context ctx, AttributeSet attrs) { TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.FontTextView); String customFont = a.getString(R.styleable.FontTextView_customFont); setCustomFont(ctx, customFont); a.recycle(); } public boolean setCustomFont(Context ctx, String asset) { Typeface tf = null; try { tf = Typeface.createFromAsset(ctx.getAssets(),"fonts/"+ asset); } catch (Exception e) { Log.e(TAG, "Could not get typeface: "+e.getMessage()); return false; } setTypeface(tf); return true; } }

最满意答案

你应该缓存TypeFace,否则你可能会冒更大的手机内存泄漏风险 。 缓存也会增加速度,因为它总是从资产中读取的速度并不是很快。

public class FontCache { private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>(); public static Typeface get(String name, Context context) { Typeface tf = fontCache.get(name); if(tf == null) { try { tf = Typeface.createFromAsset(context.getAssets(), name); } catch (Exception e) { return null; } fontCache.put(name, tf); } return tf; } }

我给出了一个关于如何加载自定义字体和样式textviews作为类似问题的答案的完整示例 。 你似乎正在做的大部分都是正确的,但你应该像上面推荐的那样缓存字体。

You should cache the TypeFace, otherwise you might risk memory leaks on older handsets. Caching will increase speed as well since it's not super fast to read from assets all the time.

public class FontCache { private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>(); public static Typeface get(String name, Context context) { Typeface tf = fontCache.get(name); if(tf == null) { try { tf = Typeface.createFromAsset(context.getAssets(), name); } catch (Exception e) { return null; } fontCache.put(name, tf); } return tf; } }

I gave a full example on how to load custom fonts and style textviews as an answer to a similar question. You seem to be doing most of it right, but you should cache the font as recommended above.

更多推荐

本文发布于:2023-08-05 07:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428824.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   字体   内存   Memory   set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!