2011-05-19 15 views
5

Estoy tratando de agregar un String a una imagen de Drawable. Actualmente no estoy usando un Panel para dibujar y me gustaría mantenerlo así. ¿Alguna idea o necesito invocar un método onDraw()?android - agregando una cadena sobre una imagen extraíble?

Mi imagen está apareciendo con este código:

Drawable image = getResources().getDrawable(tile_types[tileType]);  
setImageDrawable(image); 

me gustaría añadir un String sobre esta imagen.

Gracias.

Respuesta

6
Drawable image = getResources().getDrawable(tile_types[tileType]); 
// Store our image size as a constant 
final int IMAGE_WIDTH = image.getIntrinsicWidth(); 
final int IMAGE_HEIGHT = image.getIntrinsicHeight(); 

// You can also use Config.ARGB_4444 to conserve memory or ARGB_565 if 
// you don't have any transparency. 
Bitmap canvasBitmap = Bitmap.createBitmap(IMAGE_WIDTH, 
              IMAGE_HEIGHT, 
              Bitmap.Config.ARGB_8888); 
// Create a canvas, that will draw on to canvasBitmap. canvasBitmap is 
// currently blank. 
Canvas imageCanvas = new Canvas(canvasBitmap); 
// Set up the paint for use with our Canvas 
Paint imagePaint = new Paint(); 
imagePaint.setTextAlign(Align.CENTER); 
imagePaint.setTextSize(16f); 

// Draw the image to our canvas 
image.draw(imageCanvas); 
// Draw the text on top of our image 
imageCanvas.drawText("Sample Text", 
         IMAGE_WIDTH/2, 
         IMAGE_HEIGHT/2, 
         imagePaint); 
// This is the final image that you can use 
BitmapDrawable finalImage = new BitmapDrawable(canvasBitmap); 
17

respuesta de Sam fue mi punto de partida, pero la imagen no se presentaron, sólo el texto (lo uso en un mapa de Google). Finalmente lo conseguí trabajando con un LayerDrawable. Aquí está mi solución:

private Drawable createMarkerIcon(Drawable backgroundImage, String text, 
            int width, int height) { 

    Bitmap canvasBitmap = Bitmap.createBitmap(width, height, 
              Bitmap.Config.ARGB_8888); 
    // Create a canvas, that will draw on to canvasBitmap. 
    Canvas imageCanvas = new Canvas(canvasBitmap); 

    // Set up the paint for use with our Canvas 
    Paint imagePaint = new Paint(); 
    imagePaint.setTextAlign(Align.CENTER); 
    imagePaint.setTextSize(16f); 

    // Draw the image to our canvas 
    backgroundImage.draw(imageCanvas); 

    // Draw the text on top of our image 
    imageCanvas.drawText(text, width/2, height/2, imagePaint); 

    // Combine background and text to a LayerDrawable 
    LayerDrawable layerDrawable = new LayerDrawable(
      new Drawable[]{backgroundImage, new BitmapDrawable(canvasBitmap)}); 
    return layerDrawable; 
} 
+0

USTED sabe cómo hacerlo con un trazado 9 drawable, por lo que puede tomar el tamaño del texto? – Tsunaze

+0

¿cómo harías esto ya que BitmapDrawable ha sido depreciado?!? – M4tchB0X3r

+0

¿Sabes por qué la imagen no apareció con la respuesta de Sam? – pptang

1

Si el texto traducido se ve "angular" debido al cambio de tamaño, es mejor utilizar TextPaint en lugar de llanura Paint con estos parámetros:

TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG); 
Cuestiones relacionadas