31

कस्टम फ़ॉन्ट्स सेट करने के लिए निम्न कोड मेरे पूरे ऐप को धीमा कर देता है। मेमोरी लीक से बचने और गति बढ़ाने और स्मृति को अच्छी तरह से प्रबंधित करने के लिए मैं इसे कैसे संशोधित करूं?सेट कस्टम फ़ॉन्ट के लिए कस्टम फ़ॉन्ट के साथ मेमोरी लीक

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

उत्तर

98

आपको टाइपफ़ेस को कैश करना चाहिए, अन्यथा you might risk memory leaks on older handsets। कैशिंग गति को भी बढ़ाएगी क्योंकि यह हर समय संपत्ति से पढ़ने के लिए सुपर फास्ट नहीं है।

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

मैंने full example on how to load custom fonts and style textviews as an answer to a similar question दिया। ऐसा लगता है कि आप इसे अधिकतर सही कर रहे हैं, लेकिन आपको उपरोक्त अनुशंसित फ़ॉन्ट को कैश करना चाहिए।

+0

मैं setcustomfont के लिए fontcache i nmy code कैसे कॉल करूं? मैं इसे हर बार सही नहीं कर सकता –

+6

tf = Typeface.createFromAsset (ctx.getAssets(), "fonts /" + परिसंपत्ति) को बदलें; tf = FontCache.get ("फोंट /" + संपत्ति, सीटीएक्स) के साथ; – britzl

+1

क्या हैशटेबल का उपयोग करने का कोई कारण है? यदि नहीं, तो 'ArrayMap' कम स्मृति खपत और पुनरावृत्ति गति के कारण एक बेहतर प्रकार हो सकता है। – wilkas

1

मुझे लगता है कि फ़ॉन्ट कैश का उपयोग करने की आवश्यकता नहीं है। क्या हम इस तरह से कर सकते हैं?

उपरोक्त कोड में मामूली परिवर्तन, अगर मैं गलत हूं तो मुझे सही करें।

public class FontTextView extends TextView { 
    private static final String TAG = "FontTextView"; 
    private static Typeface mTypeface; 

    public FontTextView(Context context) { 
     super(context); 
    } 

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

    public FontTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     if (mTypeface == null) { 
      mTypeface = Typeface.createFromAsset(context.getAssets(), GlobalConstants.SECONDARY_TTF); 
     } 
     setTypeface(mTypeface); 
    } 

    } 
+1

यदि आपके पास केवल एक फ़ॉन्ट होगा, तो यह समाधान काम करेगा। कई समाधान कई फ़ॉन्ट मामलों के लिए बेहतर हैं। –

+1

हां, लेकिन मुझे लगता है कि एकल एंड्रॉइड एप्लिकेशन के लिए अधिक फोंट होने की अनुशंसा नहीं की जाती है। – Manikanta

+0

मैंने कभी ऐसा कुछ नहीं सुना। यदि आपके पास संसाधन है, तो मैं पढ़ना चाहूंगा –

संबंधित मुद्दे