将字体文件DroidSansFallback.ttf放到apk源码的src/main/assets/fonts目录下面,在使用到TextSymbol的setFontFamily方法时,将DroidSansFallback.ttf拷贝到磁盘的具体某个路径下面,然后setFontFamily就是改文件的绝对路径。示例代码如下:
##PathUtil.java【获取磁盘指定存储路径】
```
package cn.gtmap.onemap.mobile.utils;
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【将字体文件拷贝到指定目录中并返回绝对路径】
使用如下代码可预览标注的中文效果:
TextSymbol textSymbol =new TextSymbol(20, "标注文字内容", Color.RED);
textSymbol.setFontFamily(CopyFontFileUtil.getChineseFontPath(AndroidUtil.getExtraDirPath("fonts")));
Graphic textGraphic =new Graphic(centerPoint, textSymbol);
graphicsLayer.addGraphic(textGraphic);
字体文件提取地址:
链接:https://pan.baidu.com/s/1VoCp7xHZxx-c34X6bIjteg
提取码:3inl