带有位图文本覆盖的Android Drawable标记太小 [英] Android Drawable Marker with Bitmap Text Overlay too Small

查看:86
本文介绍了带有位图文本覆盖的Android Drawable标记太小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建带有文本的标记,但是该文本仅显示3个字符且非常小,它位于位图图像的右侧.我希望文本横穿图标的中间以及大字体.我手动将setFontsize增大到更大的尺寸不起作用,并且drawText的宽度和高度仍然不起作用.

I am creating a Marker with text but the text is showing only 3 characters and very small and it is right of the bit map image. I want the text to go across the middle of the icon and it big font. I manually increased setFontsize to larger size did not work and also drawText width and height still did not work.

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

推荐答案

这是文本覆盖范围问题的解决方案.用所有这些行替换一行:

Here is the solution for the text overlay bound problem. Replace one line with all this lines:

// 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

这篇关于带有位图文本覆盖的Android Drawable标记太小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆