android canvas custom font fill transparent areas - android

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?

Related

canvas drawText and emoji alpha color

the design recommendation for android regarding text legibility (https://material.io/design/color/text-legibility.html#legibility-standards) says to use for the font a black color with Alpha 87% or 60% depending on the importance of the text. So I paint text on my canvas like this :
paint.setColor(#99000000);
canvas.drawText('a text 🚫 with emoji', x, y, paint);
This work well for the text, It looks a little grayed and not pure black, however, the emoji 🚫 is also painted with the alpha and for a colored image, this is not good to use Alpha to draw it :(
On iOS, for example, emoji are drawn without taking care of the alpha of the font color. Is there any way under Android to draw a text on the canvas with font color alpha that will be applied only to the text and not for the emoji contained in the text?

Text drawn on canvas get blurr after applying scaling and translation in android?

I have two Imageview One is BaseImage View and Another is StickerImageview,for merging both of them I applied scale and translation on matrix but after that image gets blurr to some extent and also text drawn on canvas get blurr.Also set the properties of paint like this,but there is no effect on text blurr.
textPaint.setAntiAlias(false);
textPaint.setFilterBitmap(true);
textPaint.setDither(true);
textPaint.setFlags(Paint.FILTER_BITMAP_FLAG);

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.

Custom font rendering on android 4.4 has hollow transparent text

Something weird is happening on android 4.4 with one of my apps. I am using a custom view in which I set a custom typeface with Typeface.createFromAsset. The rendering works fine on the device/emulator as you can see in the image above. The problem appears when I save the content of the custom View as a bitmap:
Bitmap currentBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(currentBitmap);
view.draw(canvas);
Everything works fine on android < 4.4. The saved image looks the same as the one visible on the screen. However, on 4.4 I only see the font borders and the inside of text is transparent.
How can I fix this ?
When drawing text on a canvas in API 19 and above, you have to be mindful of the stroke and fill for the Paint object that you're using. In prior versions of Android, text was always drawn filled, even when using the stroke style. In KitKat, setting the style to stroke will do exactly that; stroke the text rather than fill it. You will need to manually set the paint style to fill when rendering text and then re-set it to stroke for drawing other things (if that's what you're trying to do).
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mTextPaint.setStyle(Style.STROKE);
// do other setup on Paint object
// Draw non-text stuff
mTextPaint.setStyle(Style.FILL);
// do other setup on Paint object
// Draw all the text stuff
}

How to draw BIG thin text in android

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

Categories

Resources