Arcgis for android TextSymbol中文乱码处理

2 篇文章 0 订阅
1 篇文章 0 订阅

将字体文件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

最终显示效果如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值