我正在使用 Canvas 创建一个 Drawable,其中包含一些背景和文本。这个 Drawable 被用作 EditText 中的复合 Drawable。
文本是通过在 Canvas 上调用 drawText() 进行绘制的,但在某些情况下,我有一个关于绘制文本的 y 位置的问题。在这些情况下,一些字符的部分被剪切掉了(请查看图片链接)。
没有定位问题的字符:
http://i50.tinypic.com/zkpu1l.jpg
存在定位问题的字符,文本包含 'g'、'j'、'q' 等:
http://i45.tinypic.com/vrqxja.jpg
您可以在下面找到重现此问题的代码片段。
有没有专家知道如何确定 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();
}
}