一:从assets中加载
将相关ttf字体库放入assets/fonts文件夹下,注意ttf文件名称不能是中文
// 加载assets中的字体
TextView textView1 = (TextView) findViewById(R.id.textView1);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/1.ttf");
textView1.setTypeface(typeface);
二:因第一种方式占用的体积很大,编译后的包会很大所以可以动态从服务器中拿到下载地址进行下载至本地后使用
下载工具类
public class EditImageFontUtls {
/**
* 获取对应字体
*
* @param context
* @param name 存储本地名称(英文命名)
* @return url 下载地址
*/
public static void getTypeface(Activity context, String name, String url, OnDownloadListener onDownloadListener) {
try {
boolean isExists = fileIsExists(getFontPath(context) + name + ".ttf");
if (isExists) {
if (onDownloadListener != null) {
onDownloadListener.onTypeface(Typeface.createFromFile(getFontPath(context) + name + ".ttf"));
}
} else {
downLoadFont(context, name, url, new OnDownloadListener() {
@Override
public void onTypeface(Typeface typeface) {
if (onDownloadListener != null) {
onDownloadListener.onTypeface(typeface);
}
}
@Override
public void onProgress(double progress) {
//下载中
if (onDownloadListener != null) {
onDownloadListener.onProgress(progress);
}
}
});
}
} catch (Exception e) {
}
}
//fileName 为文件名称 返回true为存在
public static boolean fileIsExists(String fileName) {
try {
File f = new File(fileName);
if (f.exists()) {
Log.i("fileIsExists", "有这个文件");
return true;
} else {
Log.i("fileIsExists", "没有这个文件");
return false;
}
} catch (Exception e) {
Log.i("fileIsExists", "崩溃");
return false;
}
}
//获取存取字体的路径
public static String getFontPath(Context context) {
return "/data/data/" + context.getPackageName() + "/ttf_files/";
}
/**
* 下载字体库 保存在本地 例如: /data/data/包名/ttf_files/huawencaiyun.ttf
* @param context
* @param name 保存到本地文件名称(不能是中文命名)
* @param url 字体库下载地址
* @param onDownloadListener
*/
public static void downLoadFont(Activity context, String name, String url, OnDownloadListener onDownloadListener) {
new Thread(new Runnable() {
@Override
public void run() {
FileOutputStream outputStream = null;
try {
OkHttpClient client = new OkHttpClient.Builder().build();
;
Request request = (new Request.Builder()).url(url).get().build();
Call call = client.newCall(request);
Response response = call.execute();
ResponseBody body = response.body();
InputStream inputStream = body.byteStream();
long lengh = body.contentLength();
String file_name = getFontPath(context) + name + ".ttf";
File file = new File(getFontPath(context));
if (!file.exists()) {
file.mkdirs();
}
outputStream = new FileOutputStream(file_name);
int losing = 0;
byte[] bytes = new byte[1024];
while (true) {
int read = inputStream.read(bytes);
if (read == -1) {
outputStream.flush();
inputStream.close();
outputStream.close();
Log.i("downLoadFont", "下载完成***********");
Log.i("downLoadFont", "下载地址" + file_name);
context.runOnUiThread(new Runnable() {
@Override
public void run() {
if (onDownloadListener != null) {
onDownloadListener.onTypeface(Typeface.createFromFile(getFontPath(context) + name + ".ttf"));
}
}
});
return;
}
outputStream.write(bytes, 0, read);
losing += read;
double i = (double) losing / (double) lengh;
context.runOnUiThread(new Runnable() {
@Override
public void run() {
if (onDownloadListener != null) {
Log.i("downLoadFont", "下载进度:" + i * (double) 100 + '%');
onDownloadListener.onProgress(i * (double) 100);
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
需要使用字体的时候点击调用获取字体的方法,会根据情况走本地获取或者下载
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditImageFontUtls.getTypeface(context, fontStyle.get(position).ttfName, fontStyle.get(position).url, new OnDownloadListener() {
@Override
public void onTypeface(Typeface typeface) {
//设置字体库
holder.tvName.setTypeface(typeface);
}
@Override
public void onProgress(double progress) {
//下载进度
holder.progressLayout.setProgress((float) progress);
}
});
}
});
接口回调
public interface OnDownloadListener {
void onTypeface(Typeface typeface);//字体样式
void onProgress(double progress);//下载进度
}