是否有可能转换成字符串文本里面的的EditText
箱成位图?换句话说,有没有什么办法字符串文本转换成位图,这意味着该文本将显示为图像?
下面是我的code:
类TextToImage延伸活动{
保护无效的onCreate(包savedInstanceState){
//创建字符串对象转换为图像
字符串sampleText =TEXT样本;
字符串文件名=形象;
//创建一个文件对象
文件NEWFILE =新的文件(./+文件名+.JPEG);
//创建要使用的字体
字体的字体=新的字体(宋体,Font.PLAIN,11);
//创建FontRenderContext对象,它可以帮助我们测量的文本
FontRenderContext中FRC =新的FontRenderContext(空,真,真);
}
}
解决方案
您可以创建适当大小的位图,创建一个帆布为位图,然后绘制文本到它。您可以使用画图对象要测量的文本,让您知道所需的位图的大小。你可以这样做(未经测试):
公共位图textAsBitmap(字符串文本,浮TEXTSIZE,诠释文字颜色){
涂料粉刷=新的油漆();
paint.setTextSize(TEXTSIZE);
paint.setColor(文字颜色);
paint.setTextAlign(Paint.Align.LEFT);
浮基线= -paint.ascent(); //上升()是负
INT宽度=(INT)(paint.measureText(文字)+ 0.5F); // 回合
INT身高=(INT)(基线+ paint.descent()+ 0.5F);
位图图像= Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888);
帆布油画=新的Canvas(形象);
canvas.drawText(文字,0,基线,油漆);
返回形象;
}
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 display as an image?
Below is my 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);
}
}
解决方案
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) {
Paint paint = new Paint();
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;
}