キャンバスを使用して、背景とテキストを含むDrawableを作成しています。ドロウアブルは、EditText内の複合ドロウアブルとして使用されます。AndroidキャンバスdrawTextのテキストのy位置
テキストはキャンバスに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();
}
}
私は正しい方向に向いてくれてありがとう。 canvas.drawText(text、0、textBounds.height() - textBounds.bottom、paint);解決策だった – darksaga