Auto set rectangle length on canvas as per string length - android

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.

Related

change canvas size for TextView

I need use TextView with outline text. I try use it: https://stackoverflow.com/a/10294290/4181010 , but it doesn't work correctly with android:layout_width="wrap_content", because it doesn't increase canvas when add outlines.
I need to increase the canvas to include the value of strokeWidth.
I suggest I have to override onMeasure somehow, but TextView call final method setMeasuredDimension at the end of onMeasure to apply view size and I can not intervene at this point so as not to have to rewrite the whole method.
Scaling the canvas in OnDraw does not work either, because TextView scales draw text with canvas.
My solution is increase canvas and use canvas.translate() to move in right position before draw text.
And my question: How can I increase the size of the canvas with minimal intervention? Or someone know another solution for this problem?
I'm guessing you are drawing the text twice, once with the orange (manually on the canvas) and once with the white (what the TextView is drawing with super.onDraw).
Instead of drawing the larger orange manually, you could instead draw the smaller white text and leave the orange text to TextView's super.onDraw. That way everything should fit within the canvas.

android canvas drawText

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.

How can I anchor my DynamicLayout to the bottom of my canvas?

I am working on a custom View that includes frequently updated text that I want to anchor to the bottom of the canvas.
The text length changes as well, and I'd like it to wrap, moving up the screen as more lines are needed. (DynamicLayout thus seems like a solid choice for automating this)
However, I don't see any options in the docs about specifying where on my canvas the text is drawn or in which direction it should "grow".
Here is my initialization:
TextPaint subtleTextPaint = new TextPaint();
DynamicLayout dl = new DynamicLayout(text,subtleTextPaint,getWidth()
,Layout.Alignment.ALIGN_CENTER,1,0,true);
And in onDraw(), I simply pass the canvas to the DynamicLayout object like so:
dl.draw(c);
Right now, the text is drawn at the very top of the screen and word-wraps downwards as the text gets longer.
After quite a bit of searching, I found nothing that did what I wanted. So, in true hacker style, I created my own solution. By extending the DynamicLayout class and overwriting the getLineTop() function, I was able to achieve the functionality I was looking for.
I've posted the source code here.
What i think 1st you get the Width of the screen and then set the Text with the X y according to your screen dimension
that will work
snippet is following
width=canvas.getWidth();
if(width==720)
{
canvas.drawText(String, x , y, drawText);
}
else if(width==480)
{
canvas.drawText(String, x , (y, drawText);
}
else
{
canvas.drawText(String, x , (y, drawText);
}

How to get actual frame size of text rendered in TextView Android

I'm using a TextView in android, as attached in snapshot, you can see on the right side text rendered with some space left in each line.
Actually my frame width is equal to the full screen width but text gets wrapped without getting rendered fully on a line leaving spaces.
What I really need is the actual frame size of the text rendered that excludes these blank spaces at end of each line.
I hope it clears myself and let me know if you still understands situation. Appreciate your help.
maybe this will help you:
Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText().toString(), 0,
textView.length(), bounds);
no in bounds rect you have the text size that been drawn.

Can setTextSize() be density independent?

If I were to initialize a Paint, and set it's text size like so:
Paint = new Paint();
Paint.setAntiAlias(true);
Paint.setARGB(255, 255, 0, 0);
Paint.setTextSize(screenWidth/100);
//screenWidth is the width of the screen in pixels given via display metrics.
And then draw text to the canvas like so:
String text = "Hello"
canvas.drawText(text, (screenWidth/13), (screenHeight/5), Paint);
Would the text Show up in the same relative spot the same relative size regardless of screen metrics? I ask because I only have 1 device and the Emulator doesn't run very well on my multi-core machine.
What I've been doing up until this point is simply using a bitmap with the text written over a background, but my memory usage is getting quite heavy, so I'm looking to cut down on the number of bitmaps loaded.
My other option is to save the text as a bitmap with a transparent background and overlay it on a single bitmap background. But that seems to be only half as productive, as it is actually creating 1 more bitmap, just reducing the total size of all the bitmaps stored. I also don't like this idea because i'd eventually like to take more control over the object life cycle and this will make that less effective.
Also, is there any method of adding styles to text (such as complicated fonts and color patterns besides using pre-made Drawables) so that the text can be drawn to canvas? (As cheaply as possible)
NVM, Solved By Poking around all day I figured out that DP units can be called from the res folder and will give a fairly uniform position of the text. and that paint is not as customization friendly as I wish.

Categories

Resources