31

Özel yazı tiplerini ayarlamak için aşağıdaki kod tüm uygulamamı yavaşlatıyor. Bellek sızıntılarını önlemek ve hızı artırmak ve belleği iyi yönetmek için nasıl değiştirebilirim?Özel yazı tipi için özel yazı tipiyle bellek sızıyor

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

cevap

98

Aksi belirtilmedikçe, TypeFace, you might risk memory leaks on older handsets. Önbellekleme, her zaman varlıklardan okumak için süper hızlı olmadığından, hızı da artıracaktır. Bir full example on how to load custom fonts and style textviews as an answer to a similar question verdi. Çoğu doğru yapıyor gibi görünüyor, ancak yukarıda önerilen yazı tipini önbelleğe almalısınız.

+0

setcustomfont için fontcache i nmy kodunu nasıl ararım? –

+6

'ı her defasında doğru anlayamıyorum tf = Typeface.createFromAsset (ctx.getAssets(), "font /" + asset); tf = FontCache.get ("font /" + asset, ctx); – britzl

+1

'Hashtable' kullanmanın bir nedeni var mı? Değilse, ArrayMap daha düşük bellek tüketimi ve yineleme hızı nedeniyle daha iyi bir tür olabilir. – wilkas

1

Yazı tipi önbelleği kullanmak gerekmiyor. Bu şekilde mi yapıyoruz?

Yukarıdaki kodda küçük bir değişiklik yapın, Yanlışsam düzeltin.

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

Sadece bir Yazı tipine sahip olacaksanız, bu çözüm işe yarayacaktır. Diğer çözümler çoklu yazı tipi durumları için daha iyidir. –

+1

Evet, ama sanırım tek bir android uygulama için daha fazla yazı olması tavsiye edilmez.? – Manikanta

+0

Böyle bir şey duymadım. Bir kaynağınız varsa okumak isterim –

İlgili konular