防止将字符绘制为表情符号 [英] Prevent character being drawn as emoji

查看:33
本文介绍了防止将字符绘制为表情符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用drawText()将单个Unicode字符绘制到画布上。

canvas.drawText("u270cufe0e", x, y, paint);

在运行Android 7的测试设备上,它正确显示:

但在我的模拟器"运行"Android 6中,以及在运行Android 6的实际设备上,它被绘制为Emoji,而不考虑ufe0e

这当然不是我想要的,因为我想把它画成黑色,而不是粉色!有什么方法可以在绘制文本时"关闭"表情符号吗?

推荐答案

您可以尝试使用herehere所述的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: u270cufe0e, emoji: u270cufe0f"/>

    <!-- 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: u270cufe0e, emoji: u270cufe0f"/>
</LinearLayout>

如果它不能按您希望的方式工作,请从初始化行中删除.setReplaceAll(true),然后重试,看看它是否工作。

编辑:

如果您想要手动绘制表情符号文本,例如绘制到画布上,您可以使用EmojiCompat.process(...)android.text.StaticLayout来完成。我没有尝试过,所以可能会有错误,但它应该可以工作。

// assuming x, y, and paint are defined
CharSequence emoji = EmojiCompat.get().process("u270cufe0e");
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);

这篇关于防止将字符绘制为表情符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆