I need to draw single character on my view as BIG thin text as shown in image below (text as just simple lines)
But my code below gives me following image (text as shape)
Paint dPaint = new Paint();
dPaint.setAntiAlias(true);
dPaint.setDither(true);
dPaint.setColor(Color.RED);
dPaint.setTextSize(getWidth()*.8F);
dPaint.setStyle(Paint.Style.STROKE);
dPaint.setStrokeWidth(1);
canvas.drawText("A",getWidth()*.3F, getHeight()*.6F, dPaint);
I tried various paint and stroke properties but none worked out. Also I read the in java 2D text is drawn as shape.
Is there a way to draw text (character in particular) as lines rather then shape. Also text down should be BIG in size.
I think only two ways to achieve your requirement.
Use a custom font-type with your font style and use it in your application as below
TextView text = (TextView) findViewById(R.id.textView);
Typeface customFont = Typeface.createFromAsset(getAssets(), "ThinText.ttf");
text.setTypeface(customFont);
Another technique is you have to draw the shape of give character as lines by calculating the coordinates required in the path to draw that alphabet or digit.
I hope it may help you.
You increase strokewidth, then thin is increased like so:
dPaint.setStrokeWidth(5);
Related
I'm using a ttf on a canvas. Since it is used by a widget I cannot use a TextView so I use drawText with a Paint object where the paint object has all the font properties set.
However the area in and around the strokes of the font is transparent, so i see only the black strokes of the font.
I know that with a textview I could use setTextColor() but is there something similar for Paint?
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.
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.
I'm looking into methods of completely blurring a text in a textview. Read, completely unreadable (pixelate). The methods i'm using now are using shadows. But this seems very inefficient. Anyone who has a better solution?
I did not like the Paint method used in the other answers. The below code is working for me with the radius calculated depending on the font size and applying it directly on the TextView.
if (Build.VERSION.SDK_INT >= 11) {
yourTextView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
float radius = yourTextView.getTextSize() / 3;
BlurMaskFilter filter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL);
yourTextView.getPaint().setMaskFilter(filter);
You should try to use canvas instead of TextView and draw a text with blur.
There is a lot of method using BlurMaskFilter and you can use this like that :
paint.setMaskFilter(new BlurMaskFilter(float radius, BlurMaskFilter.Blur style));
You may try below methods:
Whenever you want to show blurry text, use a specialized font for
your TextView, added to asset folder of your project.
Try setting a transparent color[#00ffffff] to your text and give some shadow to it.
I hope one of the above method helps.
If you are looking to obfuscate the text you can try using TransformationMethod of TextView.
Here you have an example, is not a blurring effect but may help: https://gist.github.com/RicardAparicio/bdd6cd30410f9431e1da238202a1dfa9
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.