StaticLayout Height Measurements

I'm trying to draw multiline text into a bitmap with Latto-Reg font, and StaticLayout seems to have problems with it.

paint.setTextSize(label.fontSize); paint.setTypeface(face); StaticLayout textLayout = new StaticLayout(label.text, paint, (int)StaticLayout.getDesiredWidth(label.text, paint), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); Bitmap bitmapAux = Bitmap.createBitmap(textLayout.getEllipsizedWidth(), textLayout.getHeight(), Bitmap.Config.ALPHA_8); canvas.setBitmap(bitmapAux); canvas.save(); canvas.translate(0, textLayout.height()); textLayout.draw(canvas); canvas.restore(); 

The texture is indented above and below depending on the font and size, while the text fits perfectly in the bitmap image, this is a lot of lost memory space and makes it turn off by a random amount.

Top / bot padding wrong

I tested using a single-line drawing, and the bitmap perfectly matched the text

 paint.getTextBounds(label.text, 0, label.text.length(), rect); Bitmap bitmapAux = Bitmap.createBitmap(rect.width(), rect.height(), Bitmap.Config.ALPHA_8); canvas.drawText(label.text, -rect.left, -rect.bottom, paint); 

pixel-perfect

I tried to get all kinds of metrics from StaticLayout, and they all seem to be disconnected from the text: line borders 0, top line 0, last bottom line ... lead to the same filling problems.

EDIT: I solved the problem using a single-line offset based drawing. However, the StaticLayout class incorrectly drew several different non-standard fonts, and I want to know why.

+6
source share
2 answers

Looking at the android developper page , it looks like it is designed to handle both a multi-line case and use it next to another Layout and therefore, there is space on top of a line of text, so if you place it directly below another Layout , it will be correctly positioned . In fact, it is simply not intended for what you are trying to achieve.

In general, it may be easier to get the borders of the text from Paint.getTextBounds() to find out what degree of text will be within the Layout .

+4
source

I created a minimal working example of what I think you are trying to accomplish: creating a raster image large enough to contain text displayed through StaticLayout.

There seem to be a few things with your code:

  • You uselessly translate vertically inside a bitmap;
  • There is no height() method for StaticLayout.

Here is my result:

Application result

I added a green background to illustrate the size of the bitmap, but otherwise my code is very little different from yours:

 public void createTexture() { int width = textLayout.getEllipsizedWidth(); int height = textLayout.getHeight(); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas2 = new Canvas(bitmap); Paint p2 = new Paint(); p2.setStyle(Style.FILL); p2.setColor(Color.GREEN); canvas2.drawRect(0, 0, width, height, p2); textLayout.draw(canvas2); } 

I created a very simple custom component for drawing a bitmap:

 @Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(bitmap, 0, 0, paint); } 

It seems that perhaps you are translating to paint several textures one after another. I would recommend that you do this in your drawing method instead, translating vertically to the height of the previous texture after painting it.

+1
source

Source: https://habr.com/ru/post/958483/


All Articles