android - 在 assets/fonts 文件夹中导入 roboto 字体后,我的应用程序滚动非常糟糕

标签 android performance fonts scroll

我的应用程序运行 android Ice Cream Sandwich ,在字体文件夹中导入 roboto.ttf 和 roboto-bold.ttf 字体并使用这些字体设置四个 TextView 后,滚动 ListView 时非常(非常)慢。 有谁知道优化性能的方法?有什么提示和技巧可以提高速度吗?

我澄清在插入这几行代码之前是非常顺利的:

Typeface roboto = Typeface.createFromAsset(activity.getAssets(), "fonts/Roboto-Regular.ttf");
    Typeface robotobold = Typeface.createFromAsset(activity.getAssets(), "fonts/Roboto-Bold.ttf");
    nome.setTypeface(robotobold);
    mq.setTypeface(roboto);
    citta.setTypeface(roboto);
    prezzo.setTypeface(roboto);
    descrizione.setTypeface(roboto);

我添加了有助于字体缓存的类:

public class TypefaceCache {
  private final HashMap<String, Typeface> map;
  private Context con;
  public TypefaceCache(Context con) {
              map = new HashMap<String, Typeface>();
              this.con = con;
  }


  public Typeface getTypeface(String file) {
    Typeface result = map.get(file);
    if (result == null) {
      result = Typeface.createFromAsset(con.getAssets(), file);
      map.put(file, result);
    }
    return result;
  }
}

我通过调用类和字体

    TypefaceCache typecache = new TypefaceCache(activity);
    Typeface roboto = typecache.getTypeface("fonts/Roboto-Regular.ttf");

但结果是一样的...

最佳答案

您可以尝试缓存您的字体

public class TypefaceCache {
  private final HashMap<String, Typeface> map =
      new HashMap<String, Typeface>();

  private Typeface getTypeface(String file, Context context) {
    Typeface result = map.get(file);
    if (result == null) {
      result = Typeface.createFromAsset(context.getAssets(), file);
      map.put(file, result);
    }
    return result;
  }
}

关于android - 在 assets/fonts 文件夹中导入 roboto 字体后,我的应用程序滚动非常糟糕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11237214/

相关文章:

xaml - 无法访问 Silverlight 类库中的字体资源

ios - 如何管理文本字形边界

Android根据listview id从数据库中获取单行

java - android 套接字应用程序在模拟器上工作正常但在 android 设备上崩溃

java - 建议我找到最好的 JAVA 编程/代码实践

sql - 稀疏连接中的 PostgreSQL 慢 WHERE 子句

Android 动态壁纸因自定义字体而崩溃

android - Firebase 空指针异常

java - HttpURLConnection 读取 InputStream 两次

sql - 检查varbinary字段上的ISNULL的策略?