表情符号不随画布旋转

时间:2016-04-01 22:24:59

标签: java android drawing android-canvas

我创建了一个允许用户使用文本绘制的应用程序,包括表情符号。用户可以根据自己的喜好旋转和调整文本大小。当用户旋转包含表情符号的文本时,会发生此问题。  enter image description here

如您所见,存在不必要的重叠。我推断这是因为我画了两次文字来实现边框效果。有趣的是,如果文本大小超过一定数量,问题就解决了。这可以在最底层的测试中看到。

以下是绘制上述图像的代码:

    public void draw(Canvas c, int x, int y) {

    Rect re = new Rect();
    Paint p = new Paint();
    p.setColor(this.color);
    p.setTextSize(this.GetSize());
    p.getTextBounds(text, 0, text.length(), re);
    p.setAntiAlias(true);

    c.save();
    c.rotate(rotation_deg, x, y);

    c.drawText(text, x - re.width() / 2, y + ((re.height() - re.bottom) - re.height() / 2), p);

    p.setStyle(Paint.Style.STROKE);
    p.setColor(color2);
    c.drawText(text, x - re.width() / 2, y + ((re.height() - re.bottom) - re.height() / 2), p);

    c.restore();
}

删除第一个绘图命令可以修复表情符号问题,但之后我只得到文本笔划。

如何旋转包含表情符号的文字?

一种可能的解决方案是首先绘制位图,然后旋转位图,但这个过程会浪费ram和time。

1 个答案:

答案 0 :(得分:0)

由于您知道如何解决问题,我会调查使用Paint.setShadowLayer(float radius, float dx, float dy, int shadowColor)而不是两次绘制文字。

  

public void setShadowLayer (float radius, float dx, float dy, int shadowColor)

     

这将在主图层下方绘制阴影图层,具有指定的偏移和颜色以及模糊半径。如果radius为0,则删除阴影层。

     

可用于在文本下方创建模糊阴影。支持与其他绘图操作一起使用仅限于软件渲染管道。

     

如果阴影颜色不透明,阴影的alpha将是paint的alpha,否则阴影颜色的alpha将是。

public void draw(Canvas c, int x, int y) {

    Rect re = new Rect();
    Paint p = new Paint();
    p.setColor(this.color);
    p.setTextSize(this.GetSize());
    p.getTextBounds(text, 0, text.length(), re);
    p.setAntiAlias(true);
    p. setShadowLayer (2.0f, 2.0f, -2.0f, this.color);

    c.save();
    c.rotate(rotation_deg, x, y);

    p.setStyle(Paint.Style.FILL_AND_STROKE);
    //p.setStyle(Paint.Style.FILL);

    p.setColor(color2);
    c.drawText(text, x - re.width() / 2, y + ((re.height() - re.bottom) - re.height() / 2), p);

    c.restore();
}