我正在使用View中的Emoji unicode。在大多数设备上,图像看起来都很好,但是在我的一款低端设备(安卓2.3)上,它们被渲染成小方块。
我能检查设备是否支持Emoji吗?这样我就可以发布我的apk,而不会在某些设备上显示那些难看的方块。
我能检查设备是否支持Emoji吗?这样我就可以发布我的apk,而不会在某些设备上显示那些难看的方块。
![enter image description here](https://istack.dev59.com/NfDki.webp)
List<String>
并过滤掉设备无法呈现的表情符号(即,如果设备旧且不支持呈现它们)。Paint
来测量文本宽度。Paint mPaint = new Paint();
private boolean emojiRenderable(String emoji) {
float width = mPaint.measureText(emoji);
if (width > 7) return true;
return false;
}
width > 7
的部分特别不规范,我原本期望非渲染表情符号的值为 0.0
,但在几个设备上,我发现这个值实际上在非渲染表情符号中的范围约为 3.0
到 6.0
,而在渲染表情符号中的范围为 12.0
到 15.0
。您的结果可能会有所不同,因此您可能需要进行测试。我认为字体大小也会影响measureText()
的输出结果,所以要记住这一点。
总的来说,我不确定这是否是一个很好的解决方案,但这是我目前想出的最好的方案。
受到上述文件中发现的两种方法的启发。
public static boolean canShowEmoji(String emoji) {
Paint paint = new Paint();
float tofuWidth = paint.measureText("\uFFFE");
float standardWidth = paint.measureText("\uD83D\uDC27");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return paint.hasGlyph(emoji);
} else {
float emojiWidth = paint.measureText(emoji);
return emojiWidth > tofuWidth && emojiWidth < standardWidth * 1.25;
// This assumes that a valid glyph for the cheese wedge must be greater than the width
// of the noncharacter.
}
}
<img src='...' />
,并使用HTML.fromHTML。这相当于杀鸡焉用牛刀。 - Phantômaxx