Android将文本绘制成位图 - 特殊字符的问题

时间:2017-02-15 21:55:37

标签: android canvas text paint

我很难理解如何将子弹点渲染成位图。

这是我到目前为止的代码:

public Bitmap textIntoBitmap(String text, float textSize) {
    Paint paint = new Paint();
    paint.setTextSize(textSize);
    paint.setColor(Color.BLACK);
    paint.setTextAlign(Paint.Align.LEFT);
    paint.setAntiAlias(true);
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);

    int width = (int) (paint.measureText(text));
    int height = (int) (bounds.height());

    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);       
    canvas.drawText(text, 0, bounds.height(), paint);
    return image;
}

现在,据我所知,这应该将文本对齐到位图的顶部。这似乎适用于普通字母,所以....所以,如果我的字符串是'这是一个测试'....

enter image description here

正如你所看到的,边界高度是50px,所以我使用边界高度创建位图,如果我使用canvas.drawText方法在Y为零绘制它,它将使用底边绘制就像位图的顶部一样:

enter image description here

因此,我在边界高度绘制它,它完全位于位图中,如下所示:

enter image description here

但是,如果我的字符串只是一个项目符号(•),我会得到一些奇怪的结果:

enter image description here

正如您所看到的,返回的边界高度为20px(我拍了一个屏幕截图并手动计算了像素数,而且确实高出了20像素)。

但是,如果我尝试在+界限高度绘制它,我会得到这个:

enter image description here

正如您所看到的那样,只能吸取一半的子弹点。我认为这与子弹不是一个全高的角色并且在正常角色所占据的空间的中间(垂直)上“漂浮”这一事实有关。这可以解释为什么我需要在Y中添加更多内容来绘制角色以查看更多内容。但是再一次,getTextBounds只返回20px,即可见字符的实际大小而不是任何空格,所以这看起来很矛盾。

如果有人能解释如何绕过这个

,将不胜感激

0 个答案:

没有答案