Typeface-为自定义字体提供字体内存缓存
Android 上自定义字体的代码一般如下:
TextView textview = (TextView) findViewById(R.id.your_referenced_textview);
// adjust this line to get the TextView you want to change Typeface typeface = Typeface.createFromAsset(getAssets(),"SourceSansPro-Regular.ttf"); // create a typeface from the raw ttf
textview.setTypeface(typeface); // apply the typeface to the textview
onCreate() 方法中进行调用。如果你只想用在单个实例上那么这种方法是足够的,但是如果你想要给app中成千上万的view都使用自定义字体的话,这可能就不是一个好方法了,毕竟我们不可能在每个初始化的地方都去加上上面那一段
提供字体内存缓存
public class FontCache {
private static HashMap<String, Typeface> fontCache = new HashMap<>();
public static Typeface getTypeface(String fontname, Context context) {
Typeface typeface = fontCache.get(fontname);
if (typeface == null) {
try {
typeface = Typeface.createFromAsset(context.getAssets(), fontname);
} catch (Exception e) {
return null;
}
fontCache.put(fontname, typeface);
}
return typeface;
}
}
缓存下字体就能让我们不用一直去操作 Assets 文件夹,接下来就能实现一个继承自 TextView 的类。
继承TextView
public class EatFoodyTextView extends TextView {
public EatFoodyTextView(Context context) {
super(context);
applyCustomFont(context);
}
public EatFoodyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context);
}
public EatFoodyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyCustomFont(context);
}
private void applyCustomFont(Context context) {
Typeface customFont = FontCache.getTypeface("SourceSansPro-Regular.ttf", context);
setTypeface(customFont);
}
}
开始的三个都是构造函数,里面都调用了 applyCustomFont() 方法,然后从上面的 FontCache 类中拿到缓存的字体文件,这样就不用每个view都去重复的从 Assets中取字体,节约了资源,最后将取到的字体设置到 setTypeface() 中。
使用自定义类
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <com.futurestudio.foody.views.EatFoodyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/eat_foody_green_dark"
android:textSize="20sp"
android:text="Future Studio Blog"
android:layout_marginBottom="24dp"/> </RelativeLayout>
<TextView/> 替换成 <com.futurestudio.foody.views.EatFoodyTextView/> ,这个前面的是全部的包名,然后就会自己应用字体。但是使用中会发现,虽然一些TextView的属性比如
textSize 能正常的显示,但是 textStyle 这个属性并不能正常的生效。添加每个ttf文件
首先将同一个系列的三种样式的 ttf 文件都加到 assets 中

在XML中使用textStyle属性
在前面已经讲解了自定义view的使用
<io.futurestud.tutorials.customfont.CustomFontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text="http://futurestud.io/blog/"
android:textSize="18sp"
android:textStyle="bold"/>
android:textStyle 而不是用一个自定义的属性 customfont:textStyle 。但是像上面这样的属性直接放上去肯定是不能生效的,还需要再加一些代码才行。提示:如果你对使用
customfont:textStyle 的方式比较感兴趣,那么下一篇文章中会介绍如何使用。实现CustomFontTextView
为了能继续的使用系统的 android:textStyle 属性,需要一些步骤。
首先需要在代码拿到这个属性的信息,这只需要一行代码:
int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);
attr 这个值是来自 TextView 的第二个构造函数中的参数,我们可以使用这个对象的 getAttributeIntValue() 方法获取XML的属性。先看一下上面代码中的
ANDROID_SCHEMA 这个参数,这个是一个常量,定义在XML的最顶部中(xmlns:android="http://schemas.android.com/apk/res/android" ),第二个参数就是定义的属性名,最后一个参数是默认值,如果这个属性没有设置,那么就会选择 Typeface.NORMAL。当我们考虑了样式之后,完善一下代码,全部的代码看起来就像下面这样。
public class CustomFontTextView extends TextView {
public static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android";
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context, attrs);
}
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyCustomFont(context, attrs);
}
private void applyCustomFont(Context context, AttributeSet attrs) {
int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);
Typeface customFont = selectTypeface(context, textStyle);
setTypeface(customFont);
}
private Typeface selectTypeface(Context context, int textStyle) {
/*
* information about the TextView textStyle:
* http://developer.android.com/reference/android/R.styleable.html#TextView_textStyle
*/
switch (textStyle) {
case Typeface.BOLD: // bold
return FontCache.getTypeface("SourceSansPro-Bold.ttf", context);
case Typeface.ITALIC: // italic
return FontCache.getTypeface("SourceSansPro-Italic.ttf", context);
case Typeface.BOLD_ITALIC: // bold italic
return FontCache.getTypeface("SourceSansPro-BoldItalic.ttf", context);
case Typeface.NORMAL: // regular
default:
return FontCache.getTypeface("SourceSansPro-Regular.ttf", context);
}
}
这样我们的自定义字体就能使用标准的应用字体样式 textStyle。
看看成果
首先我们在XML中写一些布局,包括原生的 Roboto 字体以及不同形式的自定义字体。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text="http://futurestud.io/blog/"
android:textSize="18sp"/> <io.futurestud.tutorials.customfont.CustomFontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text="http://futurestud.io/blog/"
android:textSize="18sp"/> <io.futurestud.tutorials.customfont.CustomFontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text="http://futurestud.io/blog/"
android:textSize="18sp"
android:textStyle="bold"/> <io.futurestud.tutorials.customfont.CustomFontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text="http://futurestud.io/blog/"
android:textSize="18sp"
android:textStyle="italic"/> </LinearLayout>
这个文件在模拟器上显示的效果就像下面这样。

Typeface-为自定义字体提供字体内存缓存的更多相关文章
- 最佳内存缓存框架Caffeine
Caffeine是一种高性能的缓存库,是基于Java 8的最佳(最优)缓存框架. Cache(缓存),基于Google Guava,Caffeine提供一个内存缓存,大大改善了设计Guava's ca ...
- web自定义炫酷字体
电脑有已经安装好的字体,但是如果你有特殊需要而要选择其他字体,则需要以下几个步骤 1.寻找适合你的字体 有下面几个站点提供字体下载: www.theleagueofmoveabletype.com w ...
- muse-ui底部导航自定义图标和字体颜色
最近在鼓捣用vue.js进行混合APP开发,遍寻许久终于找到muse-ui这款支持vue的轻量级UI框架,竟还支持按需引入,甚合萝卜意! 底部导航的点击波纹特效也是让我无比惊喜,然而自定义图标和字体颜 ...
- vue学习(十八)使用自定义指令 为字体渲染颜色
<div id="app"> //v-color 是自定义的 <input type="text" class="form-cont ...
- .NET/ASP.NETMVC 大型站点架构设计—迁移Model元数据设置项(自定义元数据提供程序)
阅读目录: 1.需求背景介绍(Model元数据设置项应该与View绑定而非ViewModel) 1.1.确定问题域范围(可以使用DSL管理问题域前提是锁定领域模型) 2.迁移ViewModel设置到外 ...
- 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性
基于.net的分布式系统限流组件 在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...
- cache4j轻量级java内存缓存框架,实现FIFO、LRU、TwoQueues缓存模型
简介 cache4j是一款轻量级java内存缓存框架,实现FIFO.LRU.TwoQueues缓存模型,使用非常方便. cache4j为java开发者提供一种更加轻便的内存缓存方案,杀鸡焉用EhCac ...
- C#开源磁盘/内存缓存引擎
前言 昨天写了个 <基于STSdb和fastJson的磁盘/内存缓存>,大家可以先看看.下午用到业务系统时候,觉得可以改进一下,昨晚想了一个晚上,刚才重新实现一下. 更新 1. 增加了对批 ...
- 基于STSdb和fastJson的磁盘/内存缓存
更新 1. 增加了对批量处理的支持,写操作速度提升5倍,读操作提升100倍 2. 增加了对并发的支持 需求 业务系统用的是数据库,数据量大,部分只读或相对稳定业务查询复杂,每次页面加载都要花耗不少时间 ...
随机推荐
- [Linux] Ubuntu下的文件比较工具--meld
在ubuntu中需要比较文件的差异,于是安装meld apt-get install meld 安装完后,在/usr/bin/下找到meld,然后发送到桌面上, 或者在命令行执行meld命令 打开后选 ...
- [gevent源代码分析] 深度分析gevent执行流程
一直对gevent执行流程比較模糊,近期看源代码略有所得.不敢独享.故分享之. gevent是一个高性能网络库,底层是libevent,1.0版本号之后是libev.核心是greenlet.geven ...
- oracle分析函数之windowing_clause--rows
Some analytic functions allow the windowing_clause. In the listing of analytic functions at the end ...
- Web开发中的6个坏习惯
在 Usersnap,我们在能很好的组织网站开发有超过20(总和)年的经验.我们认为这些过去的经验能让我们很好的分辨出什么是好.坏和丑陋的网站开发.如今我们不想把注意力放在消极的部分,但就这一次,我们 ...
- Thinkphp学习笔记7-输入变量
在Web开发过程中,我们经常需要获取系统变量或者用户提交的数据,这些变量数据错综复杂,而且一不小心就容易引起安全隐患,但是如果利用好ThinkPHP提供的变量获取功能,就可以轻松的获取和驾驭变量了. ...
- 给你的webstorm添加快速生成注释得快捷键
打开File----setting-map-搜搜"fix doc"
- Jquery重新学习之九[Ajax运用总结C]
前两篇文章主要介绍Jquery如何利用Ajax进行操作数据,主要介绍调用的方法:其中Jquery.ajax()是Jquery中最底层的方法:Jquery还定义的一个方法跟几个事件为Jquery.aja ...
- Jquery重新学习之四[核心属性与文档处理属性]
1:核心.each(callback),size(),length(),get([index]) 1.1 .each(callback)通过它可以遍历对象.数组的属性值并进行处理 <form i ...
- PHP开发学习门户第三版UI正式上线
官网:http://www.phpthinking.com/ 论坛:http://bbs.phpthinking.com/ 迭代.迭代,似魔鬼的步伐.似魔鬼的步伐-- PHP开发学习门户第二版UI用了 ...
- jquery remove()不兼容问题解决方案
jquery remove()不兼容问题解决方案 CreationTime--2018年7月27日10点19分 Author:Marydon 1.情景展示 点击关闭,将这个div移除掉 源码展示 ...