android - 防止字符被绘制为表情符号
问题描述
我想将单个 unicode 字符绘制到画布上,使用drawText()
.
canvas.drawText("\u270c\ufe0e", x, y, paint);
在运行 Android 7 的测试设备上,它正确显示:
但是在我的模拟器中,“运行”Android 6,以及在运行 Android 6 的真实设备上,它被绘制为表情符号,无论\ufe0e
:
这当然不是我想要的,因为我想把它画成黑色,而不是粉红色!绘制文本时有什么方法可以“关闭”表情符号吗?
解决方案
您可以尝试使用此处和此处描述的 EmojiCompat 库。将其添加到您的依赖项中;
dependencies {
...
implementation "com.android.support:support-emoji:27.1.1"
implementation "com.android.support:support-emoji-bundled:27.1.1"
...
}
初始化库;
EmojiCompat.init(new BundledEmojiCompatConfig(this).setReplaceAll(true));
然后替换TextView
为EmojiTextView
. 这是一个可用于测试的示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="HardcodedText">
<!-- replace -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="TextView - default: \u270c, text: \u270c\ufe0e, emoji: \u270c\ufe0f"/>
<!-- with -->
<android.support.text.emoji.widget.EmojiTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="EmojiTextView - default: \u270c, text: \u270c\ufe0e, emoji: \u270c\ufe0f"/>
</LinearLayout>
如果它不按您的意愿工作,请.setReplaceAll(true)
从初始化行中删除,再试一次,看看是否有效。
编辑:
如果您想手动将表情符号文本绘制到画布上,您可以使用EmojiCompat.process(...)
and来完成android.text.StaticLayout
。我还没有尝试过,所以可能会有错误,但它应该可以工作。
// assuming x, y, and paint are defined
CharSequence emoji = EmojiCompat.get().process("\u270c\ufe0e");
StaticLayout layout = new StaticLayout(emoji, paint,
canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
canvas.translate(x, y);
layout.draw(canvas);
canvas.translate(-x, -y);
推荐阅读
- php - 使用 handler.postDelayed (Android) 创建实时应用程序?
- jmeter - 在高斯分布中生成http请求
- python - 使用 python 从 FQDN 获取 url
- python - python 3中的EOL是什么意思?
- reactjs - React Hook "useState" 在函数 "deviceStatus" 中被调用,它既不是 React 函数组件也不是自定义 React
- python - boost python 3.7:来自 cpp 和 python 的打印顺序错误
- swiftui - 如何通知符合协议的 ObservedObject 的更改
- javascript - 无法访问 firestore .get() 中的数据
- visual-studio - 使用来自 .net 框架的 com 暴露的 .net 核心对象
- firebase - 如何将 displayName 添加到 Firebase 用户?(颤振/飞镖)