【问题标题】:android - adding a String over a Drawable image?android - 在可绘制图像上添加字符串?
【发布时间】:2011-08-29 07:00:22
【问题描述】:

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

我的图片显示在此代码中:

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

我想在这张图片上添加String

谢谢。

【问题讨论】:

    标签: android string image draw drawable


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      Sam 的回答是我的出发点,但没有显示图片,只有文字(我在谷歌地图上使用它)。最后我得到了它与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;
      }
      

      【讨论】:

      • 你知道如何用 9 路径 drawable 做到这一点,所以它可以采用文本的大小吗?
      • BitmapDrawable 已经贬值了,你会怎么做?!?
      • 你知道为什么图像没有出现在 Sam 的回答中吗?
      • 另外,添加 imagePaint.setAntiAlias(true);为了使文字流畅
      【解决方案3】:
      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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-05
        相关资源
        最近更新 更多