2011-05-19 37 views
5

Tôi đang cố thêm String vào hình ảnh Drawable. Tôi hiện không sử dụng Panel để vẽ và tôi muốn giữ nó theo cách đó. Bất kỳ ý tưởng hoặc tôi cần phải gọi một phương pháp onDraw()?android - thêm chuỗi trên hình ảnh có thể vẽ?

Hình ảnh của tôi được hiển thị với mã này:

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

Tôi muốn thêm một String qua hình ảnh này.

Cảm ơn.

Trả lời

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

Sam câu trả lời là điểm bắt đầu của tôi, nhưng hình ảnh không xuất hiện, chỉ có văn bản (Tôi sử dụng nó trên Google Map). Cuối cùng tôi đã làm việc với một số LayerDrawable. Đây là giải pháp của tôi:

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

bạn có biết làm thế nào để làm điều đó với một đường dẫn có thể vẽ được, vì vậy nó có thể lấy kích thước của văn bản? – Tsunaze

+0

Bạn sẽ làm điều này như thế nào vì BitmapDrawable đã bị khấu hao?!? – M4tchB0X3r

+0

Bạn có biết tại sao hình ảnh không hiển thị với câu trả lời của Sam không? – pptang

1

Nếu văn bản kết quả của bạn trông "góc cạnh" do thay đổi kích thước, nó tốt hơn để sử dụng TextPaint thay vì đơn giản Paint với các thông số:

TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG); 
Các vấn đề liên quan