Android canvas drawText y 文本的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10606410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:20:12  来源:igfitidea点击:

Android canvas drawText y-position of text

androidtextcanvaspositiondrawtext

提问by darksaga

I'm using a Canvas to create a Drawable with some background and some text. The drawable is used as a compound drawable inside an EditText.

我正在使用 Canvas 创建一个带有一些背景和一些文本的 Drawable。可绘制对象用作 EditText 内的复合可绘制对象。

The text is drawn via drawText() on the canvas, but I do have an issue with the y-position of the drawn text in some cases. In those cases parts of some characters are cut off (see image links).

文本是通过画布上的 drawText() 绘制的,但在某些情况下,绘制文本的 y 位置确实存在问题。在这种情况下,某些字符的一部分会被截断(参见图片链接)。

Characters without positioning issue:

没有定位问题的字符:

http://i50.tinypic.com/zkpu1l.jpg

http://i50.tinypic.com/zkpu1l.jpg

Characters with positioning issue, text contains 'g', 'j', 'q', etc.:

有定位问题的字符,文本包含“g”、“j”、“q”等:

http://i45.tinypic.com/vrqxja.jpg

http://i45.tinypic.com/vrqxja.jpg

You can find a code snippet to reproduce the issue below.

您可以在下面找到一个代码片段来重现该问题。

Does any expert know how to determine the proper offset for the y position?

有没有专家知道如何确定 y 位置的正确偏移量?

public void writeTestBitmap(String text, String fileName) {
   // font size
   float fontSize = new EditText(this.getContext()).getTextSize();
   fontSize+=fontSize*0.2f;
   // paint to write text with
   Paint paint = new Paint(); 
   paint.setStyle(Style.FILL);  
   paint.setColor(Color.DKGRAY);
   paint.setAntiAlias(true);
   paint.setTypeface(Typeface.SERIF);
   paint.setTextSize((int)fontSize);
   // min. rect of text
   Rect textBounds = new Rect();
   paint.getTextBounds(text, 0, text.length(), textBounds);
   // create bitmap for text
   Bitmap bm = Bitmap.createBitmap(textBounds.width(), textBounds.height(), Bitmap.Config.ARGB_8888);
   // canvas
   Canvas canvas = new Canvas(bm);
   canvas.drawARGB(255, 0, 255, 0);// for visualization
   // y = ?
   canvas.drawText(text, 0, textBounds.height(), paint);

   try {
      FileOutputStream out = new FileOutputStream(fileName);
      bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
   } catch (Exception e) {
      e.printStackTrace();
   }
}

采纳答案by Tim

I think it's probably a mistake to assume that textBounds.bottom = 0. For those descending characters, the bottom parts of those characters are probably below 0 (which means textBounds.bottom > 0). You probably want something like:

我认为假设 textBounds.bottom = 0 可能是错误的。对于那些降序字符,这些字符的底部可能低于 0(这意味着 textBounds.bottom > 0)。你可能想要这样的东西:

canvas.drawText(text, 0, textBounds.top, paint); //instead of textBounds.height()

canvas.drawText(text, 0, textBounds.top, paint); //instead of textBounds.height()

If your textBounds is from +5 to -5, and you draw text at y=height (10), then you'll only see the top half of the text.

如果您的 textBounds 是从 +5 到 -5,并且您在 y=height (10) 处绘制文本,那么您将只能看到文本的上半部分。

回答by damix911

I believe that if you want to draw text near the upper left corner you should do this:

我相信如果你想在左上角附近绘制文本,你应该这样做:

canvas.drawText(text, -textBounds.left, -textBounds.top, paint);

And you can move around the text by summing the desired amount of displacement to the two coordinates:

您可以通过将所需的位移量与两个坐标相加来移动文本:

canvas.drawText(text, -textBounds.left + yourX, -textBounds.top + yourY, paint);

The reason why this works (at least for me) is that getTextBounds() tells you where drawText() would draw the text in the event that x=0 and y=0. So you have to counteract this behavior by subtracting the displacement (textBounds.left and textBounds.top) introduced by the way text is handled in Android.

这样做的原因(至少对我而言)是 getTextBounds() 告诉您在 x=0 和 y=0 的情况下 drawText() 将在何处绘制文本。因此,您必须通过减去 Android 中处理文本的方式引入的位移(textBounds.left 和 textBounds.top)来抵消这种行为。

In this answerI elaborate a little more on this topic.

这个答案中,我详细阐述了这个主题。