我试图在Drawable
图片上添加String
。我目前没有使用Panel
进行绘制,我想保持这种状态。任何想法或我是否需要调用onDraw()
方法?android - 在可绘制图像上添加一个String?
我的图像显示了这个代码:
Drawable image = getResources().getDrawable(tile_types[tileType]);
setImageDrawable(image);
我想在这个图像添加String
。
谢谢。
我试图在Drawable
图片上添加String
。我目前没有使用Panel
进行绘制,我想保持这种状态。任何想法或我是否需要调用onDraw()
方法?android - 在可绘制图像上添加一个String?
我的图像显示了这个代码:
Drawable image = getResources().getDrawable(tile_types[tileType]);
setImageDrawable(image);
我想在这个图像添加String
。
谢谢。
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);
萨姆的回答是我的出发点,但图像没有露面,只有文字(我用它在谷歌地图)。最后,我得到了它与LayerDrawable
工作。这里是我的解决方案:
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;
}
如果导致文字看起来“角”由于调整大小,最好使用TextPaint
而不是纯Paint
与这些参数:
TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG);
你知道该怎么做有9个路径可绘制,所以它可以取文本的大小? – Tsunaze 2013-07-03 12:54:01
由于BitmapDrawable已经折旧,你会怎么做?!? – M4tchB0X3r 2013-09-11 21:51:52
你知道为什么图像没有出现Sam的答案吗? – pptang 2015-09-29 05:24:56