Drawing emojis on Android canvas using unicode values(使用Unicode值在Android画布上绘制表情符号)
问题描述
我正在尝试为我的Android应用程序创建自定义视图。在OnDraw
函数中,我试图使用unicode
值绘制一个emoji,但似乎不起作用。代码如下:
public class Scale extends View {
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final static int LINE_WIDTH = 10;
...
...
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(LINE_WIDTH);
mPaint.setColor(Color.BLUE);
...
...
//This works
canvas.drawText("My text", 0.05f*width, 0.80f*height, mPaint);
//But this does NOT draw a doughnut!!
String s = new String(Character.toChars(0x1F369)); //Doughnut
canvas.drawText(s, 0.75f*width, 0.50f*height, mPaint);
}
}
有没有人知道这附近有没有工作?还是我做错了什么?
编辑[第二个问题]:通过我在下面提交的黑客攻击,我看到emoji在TextView
上绘制的TextView
中呈现,但与正常TextView上设置的emoji相比,它们明显枯燥,如下所示:
知道我在这里遗漏了什么吗?
推荐答案
不管怎么说,我已经找到了一个解决这个问题的方法,我自己也不太喜欢!在这里,我创建一个Layout
(例如LinearLayout
),并添加一个包含我的表情符号的TextView
,然后在Canvas
上绘制Layout
。
public class Scale extends View {
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final LinearLayout layout;
...
...
public Scale(final Context context, ...) {
super(context);
...
...
//Initialise the layout & add a TextView with the emoji in it
layout = new LinearLayout(context);
final TextView tv = new TextView(context);
tv.setText(new String(Character.toChars(0x1F369))); //Doughnut
layout.addView(tv);
layout.measure(50, 50);
layout.layout(0, 0, 50, 50);
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
...
...
canvas.translate(20, 20); //Translate if necessary
//Draw the layout on the canvas, draws a doughnut!!
layout.draw(canvas);
canvas.save();
canvas.restore();
}
}
如果有更好的解决方案,请发帖。
编辑
我认为StaticLayout是在画布上绘制文本的更好选择&;没有文本/表情符号变得更单调的问题。
我修改的代码(行数比以前少):
public class Scale extends View {
private final TextPaint tPaint = new TextPaint();
private final StaticLayout lsLayout;
...
...
public Scale(final Context context, ...) {
super(context);
...
...
//Initialise the layout & add a TextView with the emoji in it
String emoji = new String(Character.toChars(0x1F369))); //Doughnut
lsLayout = new StaticLayout(emoji, tPaint, 80, Layout.Alignment.ALIGN_CENTER, 1, 1, true);
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
...
...
canvas.translate(20, 20); //Translate if necessary
//Draw the layout on the canvas, draws a doughnut as bright as the rest of the canvas!!
lsLayout.draw(canvas);
canvas.save();
canvas.restore();
}
}
这就是结果,表情符号和画布上的其他图形一样明亮:
这篇关于使用Unicode值在Android画布上绘制表情符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用Unicode值在Android画布上绘制表情符号


- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01