2011-05-19 5 views
5

J'essaie d'ajouter un String à une image Drawable. Je n'utilise pas actuellement de Panel pour dessiner et j'aimerais le garder comme ça. Des idées ou dois-je appeler une méthode onDraw()?android - ajouter une chaîne sur une image Drawable?

Mon image est Révéler avec ce code:

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

Je voudrais ajouter un String sur cette image.

Merci.

Répondre

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

La réponse de Sam était mon point de départ, mais l'image ne montrent pas, seul le texte (je l'utilise sur une carte Google Map). Enfin je l'ai eu en travaillant avec un LayerDrawable. Voici ma solution:

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

ne savez-vous comment faire avec un chemin 9 dessinable, de sorte qu'il peut prendre la taille du texte? – Tsunaze

+0

comment feriez-vous cela puisque BitmapDrawable a été déprécié?!? – M4tchB0X3r

+0

Savez-vous pourquoi l'image n'apparaît pas avec la réponse de Sam? – pptang

1

Si votre texte résultat semble « angulaire » en raison de redimensionnement, il est préférable d'utiliser TextPaint au lieu de Paint simple avec ces paramètres:

TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG); 
Questions connexes