代码之家  ›  专栏  ›  技术社区  ›  Curious

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

  •  3
  • Curious  · 技术社区  · 8 年前

    我正在尝试创建一个 自定义视图 对于我的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,我看到Emojis在 TextView 利用 Canvas 但它们是显著的 迟钝的 与普通文本视图上设置的表情符号相比,如下所示:

    enter image description here

    你知道我在这里错过了什么吗?

    2 回复  |  直到 8 年前
        1
  •  2
  •   Curious    8 年前

    值得一提的是,我找到了一个破解方法,我自己也不太喜欢!在这里,我创建了 Layout (例如:。 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

        2
  •  -1
  •   Lee Hounshell    8 年前

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

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

    在: http://www.russellcottrell.com/greek/utilities/surrogatepaircalculator.htm