2012-01-10 18 views
13

¿Es posible convertir texto de cadena que está dentro de un cuadro EditText en un mapa de bits? En otras palabras, ¿hay alguna manera de convertir texto de cuerda en un mapa de bits que significa que el texto se mostrará como una imagen?Convertir texto de cadena a mapa de bits

A continuación es mi código:

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

Respuesta

56

Se puede crear un mapa de bits del tamaño apropiado, crear un lienzo para el mapa de bits, y luego dibujar el texto en él. Puede usar un objeto Paint para medir el texto para que sepa el tamaño necesario para el mapa de bits. Se puede hacer algo como esto (no probado):

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

Ted Hopp como he leído en Internet AWT no admite en el sistema operativo Android ... – shyam

+2

@shyam - estas son todas las clases de Android, no clases AWT. Están en el paquete 'android.graphics' –

+0

hey Ted Hopp utilizo canvas.drawText() para dibujar el texto en el lienzo ahora quiero moverlo al tocar el texto significa mover el texto en el movimiento del dedo ... tengo el código para moverlo imagen en la superficie del lienzo ... si es posible hacer texto como imagen, entonces puedo usarlo como imagen y mover texto en pantalla ... ¿cómo convertir texto en BitMap para moverlo a la pantalla? – shyam

0

Para única cadena que no sé, pero,

Usted recibirá Bitmap image of the whole EditText no sólo con esta cadena,

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

intente esto:

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

I've ada La respuesta de pted @TedHopp para asegurar que la imagen creada sea siempre cuadrada, que puede ser útil dependiendo de dónde se va a mostrar la imagen, como en un icono de NavigationDrawer o similar. El texto está centrado horizontalmente en el medio de la imagen.

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