I'm using a StaticLayout to draw some multiline text.
StaticLayout is convenient because it handles all the text breaking and new lining for me...
canvas.save();
canvas.clipRect(rect);
canvas.translate(rect.left, rect.top);
staticLayout.draw(canvas);
canvas.restore();
When I just draw a single line I use canvas.drawText() in combination with Paint.breakText to detemine the number of chars I can paint on my available space...
Now I need to know the exact numer of chars that fit in my StaticLayout so that I can paint the rest of the chars to another StaticLayout.
Unfortunaltely I haven't found a method that tells me how many chars fit in it.
Is there something available to get this?
Cheers,
Stefan
Related
I made a custom View using Canvas.drawText() to draw some SpannableStrings.
I call SpannableString.setSpan() when the text contains emoji I made(I don't like the default-style emoji).
I found the Canvas.drawText() does not draw the span I set and instead, it draws the default-style emoji.
I tried to call TextView.setText() passing the same SpannableString and it works.
I want to use Canvas to draw the text for performance(Otherwise, newing several TextViews while scrolling in ListView causes severe problem about performance). How can I draw SpannableString with Canvas?
Sorry for my poor English, I am not a native speaker. Thanks for advance!
EDIT: Actually I made this custom View in order to show animation of text transitions. Several texts will go across the screen horizontally. The problem I met is that the TextView's draw method causes too much time and the animation looks bad. So I want to draw the text directly using Canvas. Sadly, I switched to StaticLayout and still found the animation is jerky.
You could try adding your Spannable to a StaticLayout and draw that to a Canvas.
Something like this:
StaticLayout layout = new StaticLayout(yourSpannable, yourPaint,
yourCanvas.getWidth(), Alignment.ALIGN_NORMAL, 1, 0, false);
layout.draw(yourCanvas);
I have an array of strings and I have drawn those strings on screen using android graphics library (Canvas, Paint). Firstly, a transparent rectangle is drawn then on this rectangle I paint the strings, giving the impression of text with border line and filled.
The problem is some strings are long and some are short, how can I modify the rectangle so that it is as long and as wide as the string is? (Like WRAP_CONTENT in textView)
Currently I am using this method
canvas.drawRect(_x-10, _y-10, _x+620, _y+30, rectanglePaint);
canvas.drawText(placeName, _x, _y, textPaint); //text
If there is a better way then please do let me know.
Note: It will be used in an AR app so the text will be moving from left to right and vice versa as the mobile is moving. _x, _y for place name works perfectly in 2nd line, I want text should remain highlighted, no matter how the mobile is moving.
Paint has a measure text method that will give the width of the text if it were to be drawn with that paint. Its get font metrics will probably give you some data that you can calculate the height from. The sizing, positioning, wrapping and rendering of text is a very complicated problem and I would advise using a child TextView (with a background) in your ViewGroup rather than trying to roll your own if you're doing anything complicated.
drawText draws a text from the providing point. But I need different behaviour.
Let's call the point(x,y) as p.
How drawText works:
pMYTEXT
What I need:
MYTpEXT
So I want to draw text exactly centered with this point. Is this possible?
I think to do it I should measure text and offset the point. But how can I measure it? It has not drawn yet.
Sure. Use the Paint function measureText to get the width of the string you want to the left of point p. Subtract that length from the x position of p, and draw it there.
Set the paint alignment to center.
paint.setTextAlign(Paint.Align.CENTER)
You can also use LEFT and RIGHT.
I'm currently using the following method to draw some text to the SurfaceView:
canvas.drawText("someText", 0, 0, paint);
Yet what if the text exceeds the width of the screen? Is it possible to define a region wherein the text can be drawn?
So now when the string's width exceeds the rectangles width, the text will be formatted to fit underneath the above text and so on eg.
"sometext"
"text carried"
"on"
Based on this answer, I believe this is what the Layout subclasses are used for. Per documentation:
A base class that manages text layout in visual elements on the
screen.
For text that will be edited, use a DynamicLayout, which will be
updated as the text changes. For text that will not change, use a
StaticLayout.
And with each subclass there's a note:
This is used by widgets to control text layout. You should not need to use this class directly unless you are implementing your own widget or custom display object, or would be tempted to call Canvas.drawText() directly.
Which sounds exactly like what you're doing.
It's basically a replacement for canvas.drawText(). It can be used in this fashion:
TextPaint tPaint = new TextPaint(paint);
StaticLayout sLayout = new StaticLayout(sText, tPaint, mWidth, widthToFill, Layout.Alignment.ALIGN_CENTER, 1.2f, 1.0f, false);
canvas.save();
canvas.translate(posX, posY);
sLayout.draw(canvas);
canvas.restore();
i am drawing text on canvas using drawText() method.But when line is bigger then screen it is cutting text means if line is greater than screen size then it should come to new line,but it is not happening.Any help will be appreciated.
What you are looking for is StaticLayout.
You can use Android.text.StaticLayout class and call it's draw(Canvas) to draw text which wraps onto the next line.
The Canvas.drawText methods do not automatically handle line-wrapping. You will have to do this yourself. You could try using the breakText methods in the Paint object—not sure if these break on word boundaries, or simply on whole characters.
Try:
mTextLayout = new DynamicLayout([charseq], paint, width,
Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
This will layout your text limited to a specific width.