Tôi có một TextView tùy chỉnh, với một thuộc tính phông chữ cá nhân:Performance vấn đề trên tùy chỉnh phông chữ TextView
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;
}
}
Tôi đang sử dụng nó trong các tập tin XML của tôi với các thuộc tính customFont = "ArialRounded.ttf", và nó hoạt động khá tốt.
Tôi đang sử dụng TextViewPlus này trong một ListView, được điền bằng một ArrayAdapter.
TextViewPlus dataText = (TextViewPlus) itemView.findViewById(R.id.data_text);
dataText.setText("My data String");
Vấn đề của tôi là hiệu suất, khi tôi cuộn ListView, thật khủng khiếp! Rất chậm và đầy đủ độ trễ. Các nhà xây dựng TextViewPlus n ° 2 nó được gọi là tất cả các thời gian tôi di chuyển danh sách.
Nếu tôi thay đổi TextViewPlus trong TextView bình thường và sử dụng dataText.setTypeface (myFont), mọi thứ đều tốt và hoạt động tốt.
Làm cách nào để sử dụng TextViewPlus mà không có vấn đề về hiệu suất?
Các bạn đã thử bộ nhớ đệm của bạn 'Typeface' ở đâu đó, vì vậy nó không nhận được tạo ra mỗi lần? – jprofitt
Tôi làm bây giờ. Tôi đã thử các giải pháp của Praful Bhatnagar và nó đang làm việc rất tốt – Nifhel