change canvas size for TextView - android

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.

Related

Android Canvas drawText with SpannableString

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);

Auto set rectangle length on canvas as per string length

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.

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.

Is there anyway to get the Rect in which Bitmap is drawn on Android?

I am trying to subclass ImageView and draw something on Bitmap. However I cannot find a way to get the Rect in which Bitmap is drawn. I can only get the Rect in which ImageView is drawn by getDrawingRect(Rect) method of ImageView. Below is an illustration of what I want to get:
The Rect I want is the blue one. Thanks in advance.
The given image will be drawn in the ImageView based on the attributes given e.g., height, width, scaling factors, etc.
So that the getDrawingRect() method giving the entire area of ImageView. If you change the drawable inside ImageView the blue colored area may change based on the image properties and imageview properties. But the yellow colored area won't change because it is fixed and based on the ImageView only, its independent of image displayed.
I think no chance to get the Rect of bitmap drawn. Don't think my answer is 100% correct, it is just a suggestion only.
You may get information about blue colored area from Drawing Cache. Try it once.
I hope it may help you.

Custom class ImageView written text ends up behind setImageBitmap

hi
The custom ImageView is setting a Bitmap like this, in the constructor.
setImageBitmap(btm);
In my onDraw method im writing text like
canvas.drawText
Text is showed and I can move the text around using onTouchEvent.
But the text is behind the Bitmap.
I thought setImageBitmap(btm) would be part of the Canvas??
Any ide!
When i draw the Bitmap in onDraw like this, The screen is black!!
canvas.drawBitmap(bitmap, 0,0, null);
If you want an ImageView that also displays text and you're already handling onDraw, why not just draw the Bitmap manually to the Canvas in onDraw? That way you have control over the order of the drawing. Draw the image to the canvas first and then the text, and the text will no longer be obscured by the image.

Categories

Resources