问题描述
我使用画布来创建一些背景和一些文字被拉伸。提拉被用作一个EditText内的化合物可绘
该文本通过在画布上的drawText()绘制的,但我有一个问题,在某些情况下显示的文本的Y位置。在这种情况下的某些字符部分被剪切掉(见图片链接)。
不定位的问题特点:的
http://i50.tinypic.com/zkpu1l.jpg
与定位的问题字符,文本中包含G,J,Q,等等。的
http://i45.tinypic.com/vrqxja.jpg
您可以找到一个code段下重现问题。
有哪位高手知道如何确定合适的偏移量y位置?
公共无效writeTestBitmap(文本字符串,字符串文件名){
// 字体大小
浮fontSize的=新的EditText(this.getContext())getTextSize()。
fontSize的+ = fontSize的* 0.2F;
//油漆写的文字
涂料粉刷=新的油漆();
paint.setStyle(Style.FILL);
paint.setColor(Color.DKGRAY);
paint.setAntiAlias(真正的);
paint.setTypeface(Typeface.SERIF);
paint.setTextSize((INT)fontSize的);
//分钟。文字RECT
矩形textBounds =新的矩形();
paint.getTextBounds(文字,0,text.length(),textBounds);
//创建位图文字
位图BM = Bitmap.createBitmap(textBounds.width(),textBounds.height(),Bitmap.Config.ARGB_8888);
// 帆布
帆布油画=新的Canvas(BM);
canvas.drawARGB(255,0,255,0); //用于可视化
// Y =?
canvas.drawText(文字,0,textBounds.height(),漆);
尝试 {
FileOutputStream中出=新的FileOutputStream(文件名);
bm.com preSS(Bitmap.Com pressFormat.JPEG,100,出);
}赶上(例外五){
e.printStackTrace();
}
}
我觉得这可能是一个错误的假设,textBounds.bottom = 0。对于那些降字符,这些字符底部的部分可能小于0(即textBounds.bottom> 0)。你可能想是这样的:
canvas.drawText(文字,0,textBounds.top,油漆); //代替textBounds.height()
如果您textBounds为+5至-5,以及绘制文本为y =身高(10),那么你就只能看到文字的上半部分。
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.
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).
Characters without positioning issue:
http://i50.tinypic.com/zkpu1l.jpg
Characters with positioning issue, text contains 'g', 'j', 'q', etc.:
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?
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();
}
}
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:
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.
这篇关于文本Android的画布上的drawText y位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!