Skip to content Skip to sidebar Skip to footer

Paint.getTextBounds() Returns To Big Height

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

Solution 1:

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: enter image description here


Post a Comment for "Paint.getTextBounds() Returns To Big Height"