您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
安卓从sdcard中加载ttf字体到textView及资产文件写入到sdcard
发布时间:2021-07-23 17:05:40编辑:雪饮阅读()
从sdcard中加载ttf字体文件渲染到textView上,只需要两行代码即可解决
Typeface typeface = Typeface.createFromFile(fontDir+File.separator+font.getFontCode()+".ttf");
watermark_text.setTypeface(typeface);
这里第一行获取ttf字体,第二行给textView设置获取的这个字体。
从资产文件中写入字体到sdcard中,像前面我们将字体放在res/raw中,实际上raw可扩展性比较差不能分门别类的建立子目录,所以这次才有assets,在res同级目录建立assets/fonts并将之前的res/raw中所有字体都复制到这个新目录中
然后就可以实现一个从资产目录中fonts目录指定字体文件复制到sdcard中的方法如:
public void FontWriteSdcard(String fontCode,String fontName){
//"fonts/fang_heng_hua_li.ttf"
String fileName=fontCode+".ttf";
AssetManager assetManager = getAssets();
try {
InputStream inputStream=assetManager.open("fonts"+File.separator+fileName);
File file = new File(fontDir);
if (!file.exists()) {//如果文件夹不存在,则创建新的文件夹
file.mkdirs();
}
readInputStream(fontDir + File.separator + fileName, inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//"fonts/fang_heng_hua_li.ttf"
String fileName=fontCode+".ttf";
AssetManager assetManager = getAssets();
try {
InputStream inputStream=assetManager.open("fonts"+File.separator+fileName);
File file = new File(fontDir);
if (!file.exists()) {//如果文件夹不存在,则创建新的文件夹
file.mkdirs();
}
readInputStream(fontDir + File.separator + fileName, inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
关键字词:sdcard,字体,textView,资产文件,assets