Canvasを使用して、背景とテキストを含むDrawableを作成しています。ドローアブルは、EditText内の複合ドローアブルとして使用されます。
テキストはキャンバス上で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();
}
}