Convert String Text To Bitmap
Is it possible to convert string text that is inside an EditText box into a Bitmap? In other words, is there any way to convert string text into a Bitmap that means the text will d
Solution 1:
You can create a Bitmap of the appropriate size, create a Canvas for the Bitmap, and then draw your text into it. You can use a Paint object to measure the text so you'll know the size needed for the bitmap. You can do something like this (untested):
public Bitmap textAsBitmap(String text, float textSize, int textColor) {
Paintpaint=newPaint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setTextAlign(Paint.Align.LEFT);
floatbaseline= -paint.ascent(); // ascent() is negativeintwidth= (int) (paint.measureText(text) + 0.5f); // roundintheight= (int) (baseline + paint.descent() + 0.5f);
Bitmapimage= Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvascanvas=newCanvas(image);
canvas.drawText(text, 0, baseline, paint);
return image;
}
Solution 2:
I've adapted @TedHopp's answer to ensure the image created is always square, which can be useful depending on where the image is to be displayed, such as in a NavigationDrawer
icon or suchlike. The text is horizontally centered in the middle of the image.
publicstatic Bitmap textAsBitmap(String text, float textSize, int textColor) {
// adapted from https://stackoverflow.com/a/8799344/1476989Paintpaint=newPaint(ANTI_ALIAS_FLAG);
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setTextAlign(Paint.Align.LEFT);
floatbaseline= -paint.ascent(); // ascent() is negativeintwidth= (int) (paint.measureText(text) + 0.0f); // roundintheight= (int) (baseline + paint.descent() + 0.0f);
inttrueWidth= width;
if(width>height)height=width; else width=height;
Bitmapimage= Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvascanvas=newCanvas(image);
canvas.drawText(text, width/2-trueWidth/2, baseline, paint);
return image;
}
Solution 3:
try this :
publicstatic Bitmap drawText(String text, int textWidth, int textSize) {
// Get text dimensionsTextPainttextPaint=newTextPaint(Paint.ANTI_ALIAS_FLAG
| Paint.LINEAR_TEXT_FLAG);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(textSize);
StaticLayoutmTextLayout=newStaticLayout(text, textPaint,
textWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
// Create bitmap and canvas to draw toBitmapb= Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Config.RGB_565);
Canvasc=newCanvas(b);
// Draw backgroundPaintpaint=newPaint(Paint.ANTI_ALIAS_FLAG
| Paint.LINEAR_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
c.drawPaint(paint);
// Draw text
c.save();
c.translate(0, 0);
mTextLayout.draw(c);
c.restore();
return b;
}
Solution 4:
Stringtext="Hello world!";
Bitmapb= Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvasc=newCanvas(b);
c.drawBitmap(b, 0, 0, null);
TextPainttextPaint=newTextPaint();
textPaint.setAntiAlias(true);
textPaint.setTextSize(16.0F);
StaticLayout sl= newStaticLayout(text, textPaint, b.getWidth()-8, Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
c.translate(6, 40);
sl.draw(c);
return b
Solution 5:
For only String I don't know but,
You will get Bitmap image of the whole EditText
not only String with this,
mEditText.setCursorVisible(false);
mEditText.buildDrawingCache();
Bitmapbmp= Bitmap.createBitmap(mEditText.getDrawingCache());
Post a Comment for "Convert String Text To Bitmap"