本文介绍了机器人 - 添加一个字符串通过绘制的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个字符串添加到绘制对象图像。我目前没有使用面板来画,我想保持这种方式。任何意见或做我需要调用的OnDraw()的方法?

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?

我的图像显示了这个code:

My image is showing up with this code:

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

我想添加一个字符串过这一形象。

感谢。

推荐答案

萨姆的回答是我的出发点,但图像没有露面,只有文字(我用它在谷歌地图)。最后,我得到了它与 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;
}

这篇关于机器人 - 添加一个字符串通过绘制的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:16
查看更多