Problem z wydajnością niestandardowej czcionki TextView
Mam niestandardowy TextView, ze spersonalizowanym atrybutem czcionki:
public class TextViewPlus extends TextView {
private static final String TAG = "TextViewPlus";
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
// This is called all the time I scroll my ListView
// and it make it very slow.
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewPlus(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.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
setTypeface(tf);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
return true;
}
}
Używam go w moich plikach XML z atrybutemcustomFont = "ArialRounded.ttf"i działa całkiem dobrze.
Korzystam z tego TextViewPlus w ListView, zapełnionym ArrayAdapter.
TextViewPlus dataText = (TextViewPlus) itemView.findViewById(R.id.data_text);
dataText.setText("My data String");
Moim problemem jest to, że wydajność, gdy przewijam ListView, jest straszna! Bardzo powolny i pełen opóźnień. Konstruktor TextViewPlus nr 2 jest wywoływany cały czas i przewijam listę.
Jeśli zmienię TextViewPlus w normalnym TextView i użyjędataText.setTypeface (myFont), wszystko jest w porządku i działa dobrze.
Jak mogę używać mojego TextViewPlus bez problemu z wydajnością?