Been looking for a way to invert the text when I draw the texts on my canvas
object. I could not find any related problem here in SO. (Maybe I am using the wrong keywords.) This is what I want to achieve with drawText()
:
Is this feature available in android.graphics
package?
This code solved my issue.
String textToWrite = "Hello World";
int padding = 5;
int paddingTop = padding;
int paddingBottom = padding;
int paddingLeft = padding;
int paddingRight = padding;
Rect bounds = new Rect();
mPaint.getTextBounds(textToWrite, 0, textToWrite.length(), bounds);
mPaint.setColor(Color.WHITE);
Bitmap bitmap = Bitmap.createBitmap(
bounds.width() + paddingLeft + paddingRight,
bounds.height() + paddingTop + paddingBottom,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.BLACK);
int y = (bitmap.getHeight() + bounds.height() - padding)/2 ;
canvas.drawText(textToWrite, paddingLeft, y, mPaint);
If you want to add this inverted text to the rest of your canvas, simply draw bitmap
to your global canvas like so:
mOriginalCanvas.drawBitmap(bitmap, mCurrentX, mCurrentY, mOriginalPaint);