2012-01-10 30 views
13

是否可以將EditText框中的字符串文本轉換爲位圖?換句話說,是否有任何方法將字符串文本轉換爲位圖,這意味着文本將顯示爲圖像?將字符串文本轉換爲位圖

下面是我的代碼:

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); 
    } 
} 

回答

56

您可以創建適當大小的位圖,創建位圖畫布,然後繪製文本到它。您可以使用Paint對象來測量文本,以便知道位圖所需的大小。你可以這樣做(未經測試):

public Bitmap textAsBitmap(String text, float textSize, int textColor) { 
    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.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; 
} 
+0

泰德·霍普爲我讀了互聯網AWT在Android操作系統不支持... – shyam 2012-01-10 06:23:26

+2

@shyam - 這是所有的Android類,而不是AWT類。他們在'android.graphics'包 – 2012-01-10 06:25:25

+0

嘿特德霍普我使用canvas.drawText()在畫布上繪製文本現在我想移動它的文本接觸意味着移動手指上的文本運動..我有代碼移動圖像在畫布的表面..如果可以將文本作爲圖像,然後我可以將它用作圖像並在屏幕上移動文本..所以,如何將文本轉換爲BitMap以將其移動到屏幕上? – shyam 2012-01-10 06:49:32

0

僅適用於字符串我不知道,但是,

你會得到Bitmap image of the whole EditText這個不僅字符串,

mEditText.setCursorVisible(false); 
mEditText.buildDrawingCache(); 
Bitmap bmp = Bitmap.createBitmap(mEditText.getDrawingCache()); 
1
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 
0

試試這個:

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; 
} 
0

我已經ada pted @TedHopp的答案確保創建的圖像是總是正方形,根據要顯示圖像的位置(如NavigationDrawer圖標等)可能會有用。文字在圖像中間水平居中。

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; 
}