带有位图文字叠加的Android Drawable Marker太小了

时间:2016-02-19 02:15:12

标签: android bitmap drawable createbitmap

我正在创建一个带文字的标记,但文本只显示3个字符,非常小,它位于位图图像的右边。我希望文字在图标的中间 和大字体。我手动增加setFontsize到更大的尺寸没有工作,drawText宽度和高度仍然无法正常工作。

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

Bitmap canvasBitmap = Bitmap.createBitmap(width, height, 
                  Bitmap.Config.ARGB_8888);  //width, height,
// 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(26f); // 8f

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

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

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

我称之为此功能:

d=createMarkerIcon(getResources().getDrawable(R.drawable.pointer_bubble_selected), markerTxt, 100, 100);  //marker_green=23x37 29, 50

1 个答案:

答案 0 :(得分:0)

以下是文本叠加绑定问题的解决方案。用所有这些行替换一行:

// draw text to the Canvas center
  Rect bounds = new Rect();
  int x = (canvasBitmap.getWidth() - bounds.width())/2;
  int y = (canvasBitmap.getHeight() + bounds.height())/2;



// Draw the text on top of our image
//imageCanvas.drawText(text, width /4, height / 4, imagePaint); //OLD
imageCanvas.drawText(text, x , y, imagePaint); //NEW