Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用unicode值在Android画布上绘制表情_Java_Android_Canvas_Unicode_Emoji - Fatal编程技术网

Java 使用unicode值在Android画布上绘制表情

Java 使用unicode值在Android画布上绘制表情,java,android,canvas,unicode,emoji,Java,Android,Canvas,Unicode,Emoji,我正在尝试为我的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

我正在尝试为我的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);
    }
}
有人知道这附近有没有工作吗?还是我做错了什么

编辑[第二个问题]:根据我在下面提交的技巧,我看到表情在
画布上绘制的
TextView
中呈现,但与在普通TextView上设置的表情相比,它们显着更迟钝,如下所示:


你知道我在这里遗漏了什么吗

问题是您的表情符号超出0000-FFFF的范围 因此,它不能表示为单个unicode字符

如果您为表情符号正确编码“unicode代理项对”,这可能是可能的

有一个计算器用于确定多字节unicode的正确代理项对
在:

不管它值多少钱,我已经找到了一种破解方法,我自己也不太喜欢!在这里,我创建了一个
布局
(例如
线性布局
),并添加一个
文本视图
,其中包含我的表情符号,然后在
画布上绘制
布局

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();
    }
}
如果有更好的解决方案,请发布

编辑

我发现这是在画布上绘制文本的一个更好的选择&不会出现文本/表情变得更迟钝的问题

我的修改代码(比以前少行):

结果是,表情符号与画布上的其他图形一样明亮:


谢谢,但这似乎没有帮助:
String s=new String(Character.toChars(0xD83C))+new String(Character.toChars(0xDF69))//油炸圈饼画布。drawText(s,0.75f*宽,0.50f*高,mPaint)
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();
    }
}