在Android画布上使用Unicode值绘制表情符号

4

我正在尝试为我的Android应用程序创建一个自定义视图。在OnDraw函数中,我尝试使用表情符号的Unicode值来绘制一个表情符号,但似乎不起作用。以下是代码:

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);
    }
}

有没有人知道这里是否有解决方法?或者是我的操作有误?

编辑[第二个问题]:通过我下面提交的hack,我发现表情符号在Canvas上绘制的TextView中呈现出来,但与在普通TextView上设置的表情符号相比,它们显得明显暗淡,如下所示:

enter image description here

有什么想法吗?

2个回答

2
就此而言,我发现了一个解决方法,但我自己并不太喜欢它!在这种情况下,我创建一个布局(例如LinearLayout),添加一个包含表情符号的TextView,然后在Canvas上绘制该布局。
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();
    }
}

这是结果,表情符号和画布上的其余绘画一样鲜艳明亮。

enter image description here


-1

谢谢,但这似乎没有帮助:String s = new String(Character.toChars(0xD83C)) + new String(Character.toChars(0xDF69)) ; //甜甜圈 canvas.drawText(s, 0.75f*width, 0.50f*height, mPaint); - Curious

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接