Android Canvas.Drawtext

Android Canvas.drawText

Worked this out, turns out that android.R.color.black is not the same as Color.BLACK. Changed the code to:

Paint paint = new Paint(); 
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawPaint(paint);

paint.setColor(Color.BLACK);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);

and it all works fine now!!

What is the use of canvas' `drawTextRun`? How is it different from `drawText`?

Apparently this function drawTextRun has no use for English or any language that has characters that doesn't change (e.g. Chinese).

It is only useful for language that has letters that will change pending on the existence of its neighboring letters. A good example is Arabic.

Do refer to Arabic Unicode, https://en.wikipedia.org/wiki/Arabic_script_in_Unicode, where the language.

e.g.

The 4 letters Arabic word عربى. (note Arabic is from Right to Left)

If written individually is
ع ر ب ى

Notice the shape differs when it is by itself.

With the below code

    private val TEXT = "عربى"

canvas.drawTextRun(TEXT,
1, TEXT.length - 1,
1, TEXT.length - 1,
x, y,
true, projectResources.paint)

It will produces

Sample Image

However if we change the context length, (i.e. the shown word is not complete, but does have it's neighboring first and last letters not shown)

    private val TEXT = "عربى"

canvas.drawTextRun(TEXT,
1, TEXT.length - 1,
0, TEXT.length,
x, y,
true, projectResources.paint)

It will be as below

Sample Image

In short, despite the same letters are there, when with different context, it is writen differently.

Note: Thanks to https://hencoder.com/ui-1-3/ for shedding the light to understand the use of this method.

Android canvas drawText y-position of text

I think it's probably a mistake to assume that textBounds.bottom = 0. For those descending characters, the bottom parts of those characters are probably below 0 (which means textBounds.bottom > 0). You probably want something like:

canvas.drawText(text, 0, textBounds.top, paint); //instead of textBounds.height()

If your textBounds is from +5 to -5, and you draw text at y=height (10), then you'll only see the top half of the text.

Android Center text on canvas

Try the following:

 Paint textPaint = new Paint();
textPaint.setTextAlign(Paint.Align.CENTER);

int xPos = (canvas.getWidth() / 2);
int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
//((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center.

canvas.drawText("Hello", xPos, yPos, textPaint);

How to draw text in all caps on Canvas

toUpperCase() does not modify the original string, it creates and returns new string. You are ignoring the result of toUpperCase(), that's why it does not work for you.

Replace topText.toUpperCase(); with topText = topText.toUpperCase();. Same for bottomText

Android canvas.drawText() not showing

the y axis of base line is not 0, try this

    Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
textPaint.setColor(Color.RED);
textPaint.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));
textPaint.setTextAlign(Align.LEFT);
FontMetrics metric = textPaint.getFontMetrics();
int textHeight = (int) Math.ceil(metric.descent - metric.ascent);
int y = (int)(textHeight - metric.descent);
canvas.drawText("text", 0, y, textPaint);

Android Canvas drawText with SpannableString

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


Related Topics



Leave a reply



Submit