Ist es möglich, Text in einer EditText
-Box in eine Bitmap zu konvertieren? Gibt es also eine Möglichkeit, Text in eine Bitmap umzuwandeln, dh der Text wird als Bild angezeigt?
Unten ist mein Code:
class TextToImage extends Activity {
protected void onCreate(Bundle savedInstanceState) {
//create String object to be converted to image
String sampleText = "SAMPLE TEXT";
String fileName = "Image";
//create a File Object
File newFile = new File("./" + fileName + ".jpeg");
//create the font you wish to use
Font font = new Font("Tahoma", Font.PLAIN, 11);
//create the FontRenderContext object which helps us to measure the text
FontRenderContext frc = new FontRenderContext(null, true, true);
}
}
Sie können eine Bitmap mit der entsprechenden Größe erstellen, eine Leinwand für die Bitmap erstellen und dann Ihren Text darin zeichnen. Sie können ein Paint-Objekt verwenden, um den Text zu messen, sodass Sie die für die Bitmap erforderliche Größe kennen. Sie können so etwas tun (ungeprüft):
public 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.5f); // round
int height = (int) (baseline + Paint.descent() + 0.5f);
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawText(text, 0, baseline, Paint);
return image;
}
versuche dies :
public static Bitmap drawText(String text, int textWidth, int textSize) {
// Get text dimensions
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG
| Paint.LINEAR_TEXT_FLAG);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(textSize);
StaticLayout mTextLayout = new StaticLayout(text, textPaint,
textWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
// Create bitmap and canvas to draw to
Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Config.RGB_565);
Canvas c = new Canvas(b);
// Draw background
Paint paint = new Paint(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;
}
String text = "Hello world!";
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawBitmap(b, 0, 0, null);
TextPaint textPaint = new TextPaint();
textPaint.setAntiAlias(true);
textPaint.setTextSize(16.0F);
StaticLayout sl= new StaticLayout(text, textPaint, b.getWidth()-8, Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
c.translate(6, 40);
sl.draw(c);
return b
Ich habe die Antwort von @ TedHopp angepasst, um sicherzustellen, dass das erstellte Bild immer quadratisch ist. Dies kann nützlich sein, je nachdem, wo das Bild angezeigt werden soll, beispielsweise in einem NavigationDrawer
icon oder ähnliches. Der Text wird horizontal in der Bildmitte zentriert.
public static Bitmap textAsBitmap(String text, float textSize, int textColor) {
// adapted from https://stackoverflow.com/a/8799344/1476989
Paint paint = new 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);
int trueWidth = width;
if(width>height)height=width; else width=height;
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawText(text, width/2-trueWidth/2, baseline, Paint);
return image;
}
Für nur String weiß ich nicht, aber
Sie erhalten damit Bitmap image of the whole EditText
nicht nur String,
mEditText.setCursorVisible(false);
mEditText.buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap(mEditText.getDrawingCache());