android - 在可绘制图像上添加字符串?

发布于 2024-11-08 17:42:01 字数 343 浏览 0 评论 0原文

我正在尝试将 String 添加到 Drawable 图像。我目前没有使用 Panel 进行绘图,我希望保持这种状态。有什么想法或者我需要调用 onDraw() 方法吗?

我的图像显示有以下代码:

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

我想在此图像上添加一个 String

谢谢。

I'm trying to add a String to a Drawable image. I'm currently not using a Panel to draw and I'd like to keep it that way. Any ideas or do I need to invoke an onDraw() method?

My image is showing up with this code:

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

I'd like to add a String over this image.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小苏打饼 2024-11-15 17:42:01

萨姆的回答是我的出发点,但图像没有显示,只有文本(我在谷歌地图上使用它)。最后我让它与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;
}

Sam's answer was my starting point, but the image didn't show up, only the text (I use it on a Google Map). Finally I got it working with a LayerDrawable. Here is my solution:

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;
}
鸠魁 2024-11-15 17:42:01
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);
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);
贵在坚持 2024-11-15 17:42:01

如果您的结果文本由于调整大小而看起来“有角度”,最好使用 TextPaint 而不是带有以下参数的普通 Paint

TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG);

If your resulted text looks "angular" due to resizing, it's better to use TextPaint instead of plain Paint with these parameters:

TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文