Created
March 14, 2020 09:29
-
-
Save abilogos/86b519a437931d321747b7bd5db80136 to your computer and use it in GitHub Desktop.
Android Java Development in case of Using an Layout Element that hasnot text attribute, a string can convert to an bitmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//method to convert your text to image | |
public static Bitmap textAsBitmap(String text, float textSize, int textColor) { | |
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
paint.setTextSize(textSize); | |
paint.setColor(textColor); | |
paint.setTextAlign(Paint.Align.LEFT); | |
float baseline = -paint.ascent(); // ascent() is negative | |
int width = (int) (paint.measureText(text) + 0.0f); // round | |
int height = (int) (baseline + paint.descent() + 0.0f); | |
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(image); | |
canvas.drawText(text, 0, baseline, paint); | |
return image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Author is:
Author Github Page