如何解决android-在可绘制图像上添加字符串?
| 我正在尝试向Drawable
图片添加String
。我目前未使用Panel
绘画,我希望保持这种状态。有什么想法或需要调用an3ѭ方法吗?
我的图片显示如下代码:
Drawable image = getResources().getDrawable(tile_types[tileType]);
setimageDrawable(image);
我想在此图片上添加一个“ 0”。
谢谢。
解决方法
Sam的答案是我的出发点,但是图像没有显示,只有文字(我在Google Map上使用)。最终我得到了与
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;
}
,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);
,如果由于调整大小而导致结果文本看起来为“角”,则最好使用带有这些参数的ѭ9而不是普通Paint
:
TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。