将字体文件DroidSansFallback.ttf放到apk源码的src/main/assets/fonts目录下面,在使用到TextSymbol的setFontFamily方法时,将DroidSansFallback.ttf拷贝到磁盘的具体某个路径下面,然后setFontFamily就是字体文件DroidSansFallback.ttf的绝对路径。示例代码如下:
PathUtil.java【获取磁盘指定存储路径】
import android.content.Context;
import android.os.storage.StorageManager;
import android.text.TextUtils;
import java.io.File;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @作者 王建明
* @创建日期 2019/6/7
* @创建时间 21:01
* @版本号 V 1.0
*/
public class PathUtil {
/**
* @作者 王建明
* @创建日期 2019/6/7
* @创建时间 21:05
* @描述 —— 获取指定文件夹全路径,不存在会自动创建
*/
public static String getExtraPath(Context context,String dirName){
String path = getStoragePath(context, true);
if (TextUtils.isEmpty(path)) {
path = getStoragePath(context, null);
}
String basePath = path + File.separator + dirName;
return basePath;
}
public static String getStoragePath(Context mContext, Boolean is_removale) {
StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
Class<?> storageVolumeClazz = null;
try {
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getPath = storageVolumeClazz.getMethod("getPath");
Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
String path = (String) getPath.invoke(storageVolumeElement);
boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
if (is_removale != null && is_removale == removable) {
return path;
} else if (is_removale == null) {
return path;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
CopyFontFileUtil.java【将字体文件拷贝到指定目录中并返回绝对路径】
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* @作者 王建明
* @创建日期 2019/6/7
* @创建时间 20:08
* @版本号 V 1.0
*/
public class CopyFontFileUtil {
public static String getChineseFontPath(String baseDir) {
String fontPath = baseDir + File.separator + "DroidSansFallback.ttf";
File dataFile = new File(fontPath);
if (dataFile.exists()) {
Log.i("CopyFontFileUtil", "qwer---> 文件已存在");
} else {
Log.i("CopyFontFileUtil", "qwer---> 文件不存在,进行拷贝...");
try {
InputStream mips = AndroidUtil.context.getResources().getAssets().open("fonts/DroidSansFallback.ttf");
FileOutputStream mos = new FileOutputStream(fontPath);
byte[] buffer = new byte[1024 * 1024];
int count = 0;
while ((count = mips.read(buffer)) > 0) {
mos.write(buffer, 0, count);
}
mos.close();
mips.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return fontPath;
}
}
使用如下代码可预览标注的中文效果:
TextSymbol textSymbol = new TextSymbol(20, colStr, color);
textSymbol.setFontFamily(CopyFontFileUtil.getChineseFontPath(PathUtil.getExtraDirPath("fonts")));
Graphic textGraphic = new Graphic(centerPoint, textSymbol);
graphicsLayer.addGraphic(textGraphic);
字体文件提取地址:
链接:https://pan.baidu.com/s/1VoCp7xHZxx-c34X6bIjteg
提取码:3inl
最终显示效果如下: