2012-08-16 179 views
0

有什么方法可以加快我应用程序中字体的更改?更改ListView字体

我有一个ListView从XML在线加载其列表。这一切都很好,但我需要将其字体更改为我的资产文件夹中的自定义字体。

在i施加:

TxEventName = (TextView)row.findViewById(id.eventNameTx); 
TxEventName.setText(getEventsCount(position)); 
TxEventName.setTypeface(Utilities.textFont(this.getContext())); 

变得缓慢,因为我施加的setTypeface

有时它会先载入用户界面布局,然后是列表,有时它只是一个延迟的黑屏,然后当下一个活动出来时,列表已经存在。我甚至用AsyncTask装载值,这有点像列表

private class initList extends AsyncTask<Void, Void, Void>{ 

    @Override 
    protected Void doInBackground(Void... params) { 

     SimpleDateFormat formatterc; 
     Date xdatec=null; 
     formatterc = new SimpleDateFormat("dd/MM/yy"); 
     try { 
      xdatec = (Date)formatterc.parse(date); 
      SimpleDateFormat frmtr = new SimpleDateFormat(dateTemplateForChecking); 
      selectedDate = frmtr.format(xdatec).toString(); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     for(int i = 0; i < XMLParser.ppEvents.size(); i++) { 
      EventsObj evObj = XMLParser.ppEvents.get(i); 

      for(int j = 0; j < evObj.date.size(); j++) { 
       if(selectedDate.equalsIgnoreCase(evObj.date.get(j))) { 

        StringBuilder sbldr = new StringBuilder(); 
        sbldr.append(evObj.act_id); 
        sbldr.append("!"); 
        if (SelectLanguage.lang == "ENG"){ 
        sbldr.append(evObj.name_en); 
        } else if (SelectLanguage.lang == "TCH"){ 
         sbldr.append(evObj.name_tc); 
        } else if (SelectLanguage.lang == "SCH"){ 
         sbldr.append(evObj.name_sc); 
        } 

        mainEvents.add(sbldr.toString()); 
        eventID.add(Integer.toString(evObj.act_id)); 
       } 
      } 
     } 
     adapter = new events_adapter(PublicProg.this, layout.events_adapter, id.eventNameTx, mainEvents); 
     return null; 
    } 

    protected void onPostExecute(Void result){ 
     eventList.setAdapter(adapter); 
    } 

    protected void onPreExecute(){ 
     Bundle ext = getIntent().getExtras(); 

     position = ext.getString("position"); 
     date = ext.getString("date"); 

     mainEvents = new ArrayList<String>(); 
     eventID = new ArrayList<String>(); 
    } 
} 

回答

1

我使用它来为Typeface

public class Typefaces{ 

private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>(); 

    public static Typeface get(Context c, String name){ 
     synchronized(cache){ 
      if(!cache.containsKey(name)){ 
       Typeface t = Typeface.createFromAsset(
         c.getAssets(), 
         String.format("fonts/%s.OTF", name) 
        ); 
       cache.put(name, t); 
      } 
      return cache.get(name); 
     } 
    } 

} 

创建简单的缓存在适配器构造函数初始化您的自定义Typeface

myriadBold = Typefaces.get(mContext, "MYRIADPRO-BOLD"); 

,并用它在getView

txt.setTypeface(myriadBold); 

这种方式降低号码系统呼叫skia生成客户字体。

+0

哇。谢谢。这一次采取了我需要10秒才能加载的活动,只是稍微延迟了1秒。 – 2012-08-17 06:00:12

+0

我可以把它放在我的启动画面上来缓存我的应用程序中的字体吗? – 2012-09-03 03:05:57

0

也许你设置的字体是不是好地方。

此外Utilities.textFont(this.getContext()),与此你可能正在从文件系统中读取字体,这可能会减慢你的进程,并取决于该行被调用的位置,你正在锁定用户界面。

0

您可以通过自定义TextView

public class TextViewGeorgiaBold extends TextView { 

Resources res = getResources(); 
String font_path = res.getString(R.string.MAIN_FONT); 

public TextViewGeorgiaBold(Context context, AttributeSet attrs, int defStyle) { 
    super(context.getApplicationContext(), attrs, defStyle); 
    Typeface font = Typeface.createFromAsset(getContext() 
      .getApplicationContext().getAssets(), font_path); 
    setTypeface(font); 
} 

public TextViewGeorgiaBold(Context context, AttributeSet attrs) { 
    super(context.getApplicationContext(), attrs); 
    Typeface font = Typeface.createFromAsset(getContext() 
      .getApplicationContext().getAssets(), font_path); 
    setTypeface(font); 

} 

public TextViewGeorgiaBold(Context context) { 
    super(context.getApplicationContext()); 
    Typeface font = Typeface.createFromAsset(getContext() 
      .getApplicationContext().getAssets(), font_path); 
    setTypeface(font); 
} 

} 

设置Typeface为它设置TypeFace在布局通胀的时间。

可能会帮助你。