Paint.getTextBounds()返回到大高度 [英] Paint.getTextBounds() returns to big height
问题描述
编辑:这个问题从仿真器来了,错误并没有出现真正的设备上:(
我想画一些文本在自定义视图,必须有没有措施,但在Paint.getTextBounds()的返回值的高度大约高出30%,那么实际的文本赋予一切古怪的样子。
我发现这个: Android的油漆:.measureText()VS .getTextBounds()并试图解决方案code添加到我自己的onDraw,看到我同样的测量误差在我的code。下面是结果的图片:
比较:
图像从 Android的油漆复制:.measureText()VS .getTextBounds()
请注意在第一张照片的文本上方的间距。任何想法可能会导致什么呢?或者测量绘制字符串的高度有其他方法?
下面是的onDraw方法:
@覆盖
公共无效的onDraw(帆布油画){
// canvas.drawColor(color_Z1);
// r.set(0,0,(int)的(宽*进展),高度);
// paint.setColor(color_Z2);
//// canvas.drawRect(R,漆);
// textPaint.getTextBounds(文字,0,text.length(),R);
// canvas.drawRect(R,漆);
// canvas.drawText(文字,0,r.height(),textPaint); 最后一个String =你好,我是一些文本。! 涂料P =新的油漆();
矩形边界=新的矩形();
p.setTextSize(60); p.getTextBounds(S,0,s.length(),边界);
浮MT = p.measureText(S);
INT体重= bounds.width(); Log.i(LCG的String.format(
measureText%F,getTextBounds%D(%S),
公吨,
体重,bounds.toShortString())
);
bounds.offset(0,-bounds.top);
p.setStyle(Style.STROKE);
canvas.drawColor(0xff000080);
p.setColor(为0xffff0000);
canvas.drawRect(边界,P);
p.setColor(0xff00ff00);
canvas.drawText(S,0,bounds.bottom页);
}
我因此未测试code,但我没有看到有Paint.getTextBounds()的任何问题:
公共类TextBoundsTest扩展视图{
私人涂料粉刷;
私人矩形边界; 公共TextBoundsTest(上下文的背景下){
超级(上下文);
油漆=新的油漆(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(32);
边界=新的矩形();
} @覆盖
保护无效的onDraw(帆布油画){
字符串文本=这是我的文字;
paint.getTextBounds(文字,0,text.length(),边界);
Log.d(TAG的onDraw+界); INT X =(的getWidth() - bounds.width())/ 2;
诠释Y = 70; paint.setColor(0xff008800);
bounds.offset(X,Y);
canvas.drawRect(边界,油漆); paint.setColor(0xffeeeeee);
canvas.drawText(文字,X,Y,油漆);
}
}
在Activity.onCreate补充一点:
TextBoundsTest视图=新TextBoundsTest(本);
的setContentView(视图);
的结果为:
EDIT: The problem came from the emulator, the error did not appear on a real device :(
I'm trying to draw some text in a custom view and must there for measure it but the value of the Paint.getTextBounds() returns a height which is about 30% higher then the actual text which gives everything a quirky look.
I found this: Android Paint: .measureText() vs .getTextBounds() and tried to add the solution code to my own onDraw and saw that i the same measuring error as in my code. Here is a picture of the result:
Compare with: The image is copied from Android Paint: .measureText() vs .getTextBounds()
Note the spacing above the text in the first picture. Any Ideas what might be causing this? Or are there alternative ways to measure height of a drawn string?
Here is the onDraw method:
@Override
public void onDraw(Canvas canvas){
// canvas.drawColor(color_Z1);
// r.set(0, 0, (int)(width*progress), height);
// paint.setColor(color_Z2);
//// canvas.drawRect(r, paint);
// textPaint.getTextBounds(text, 0, text.length(), r);
// canvas.drawRect(r, paint);
// canvas.drawText(text, 0, r.height(), textPaint);
final String s = "Hello. I'm some text!";
Paint p = new Paint();
Rect bounds = new Rect();
p.setTextSize(60);
p.getTextBounds(s, 0, s.length(), bounds);
float mt = p.measureText(s);
int bw = bounds.width();
Log.i("LCG", String.format(
"measureText %f, getTextBounds %d (%s)",
mt,
bw, bounds.toShortString())
);
bounds.offset(0, -bounds.top);
p.setStyle(Style.STROKE);
canvas.drawColor(0xff000080);
p.setColor(0xffff0000);
canvas.drawRect(bounds, p);
p.setColor(0xff00ff00);
canvas.drawText(s, 0, bounds.bottom, p);
}
i didnot test your code but i dont see any problems with Paint.getTextBounds():
public class TextBoundsTest extends View {
private Paint paint;
private Rect bounds;
public TextBoundsTest(Context context) {
super(context);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(32);
bounds = new Rect();
}
@Override
protected void onDraw(Canvas canvas) {
String text = "this is my text";
paint.getTextBounds(text, 0, text.length(), bounds);
Log.d(TAG, "onDraw " + bounds);
int x = (getWidth() - bounds.width()) / 2;
int y = 70;
paint.setColor(0xff008800);
bounds.offset(x, y);
canvas.drawRect(bounds, paint);
paint.setColor(0xffeeeeee);
canvas.drawText(text, x, y, paint);
}
}
add this in Activity.onCreate:
TextBoundsTest view = new TextBoundsTest(this);
setContentView(view);
the result is:
这篇关于Paint.getTextBounds()返回到大高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!