【问题标题】:How to write text on an image如何在图像上写文字
【发布时间】:2012-07-04 07:34:13
【问题描述】:

我可以在 iPhone 上的 Objective-C 中执行此操作,但现在我正在寻找等效的 Android Java 代码。我也可以用纯 Java 来做,但我不知道 Android 特定的类是什么。我想动态生成一个PNG图像,其中一些文本位于图像中心。

public void createImage(String word, String outputFilePath){ 
    /* what do I do here? */ 
}

相关话题:

【问题讨论】:

  • 与往常一样,您的第一步是阅读开发人员指南中的图形部分

标签: java android image text png


【解决方案1】:

类似的东西呢:

Bitmap bitmap = ... // Load your bitmap here
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(); 
paint.setColor(Color.BLACK); 
paint.setTextSize(10); 
canvas.drawText("Some Text here", x, y, paint);

【讨论】:

  • 完美,正是我需要的。
  • 如果我打包这个位图,上面会有文字吗?
【解决方案2】:

您不需要使用图形。

更简单的方法是创建一个包含两个元素的FrameLayout - 用于图像的ImageView,以及用于在顶部绘制任何内容的另一个视图。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</FrameLayout>

当然,图片顶部的东西不必是简单的TextView,它可以是另一张图片,或者包含您喜欢的任意元素的另一个布局。

【讨论】:

  • 这是最简单的方法,不仅适用于静态图像,还适用于表面视图或其他任何东西
  • 我会给你一个赞成票,因为我在大多数情况下都同意你的观点。但我特别问了一个不同的问题,因为我要求它需要嵌入到图像中。
  • 您在问题的哪个地方说它需要嵌入?无论如何。
  • 好点,我很抱歉。我以为我做到了,但我应该更明确一点。
  • 可能没有明确说明,但我确实理解他想要嵌入文本。您也可以从提供的两个链接中看到这一点。所以尽管他很高兴给你投票,因为他害怕对抗他不应该有。
【解决方案3】:

我认为这也是一个很好的解决方案,可以解决您的问题。或下载source code demo

public Bitmap drawText(String text, int textWidth, int color) {

        // Get text dimensions
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.parseColor("#ff00ff"));
        textPaint.setTextSize(30);

        StaticLayout mTextLayout = new StaticLayout(text, textPaint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);

        // Create bitmap and canvas to draw to
        Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Bitmap.Config.ARGB_4444);
        Canvas c = new Canvas(b);

        // Draw background
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(color);
        c.drawPaint(paint);

        // Draw text
        c.save();
        c.translate(0, 0);
        mTextLayout.draw(c);
        c.restore();

        return b;
    }

上述函数从文本中绘制位图图像或者下载source code demo看看它是如何工作的

【讨论】:

    【解决方案4】:

    GETah 的答案对我不起作用,所以我稍微调整了一下,这是一个 Kotlin 版本,它使用 TextPaint

    val canvas = Canvas(bm)
    val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG or Paint.LINEAR_TEXT_FLAG)
    textPaint.style = Paint.Style.FILL
    textPaint.color = Color.BLACK
    textPaint.textSize = 30f
    canvas.drawText("Hello", 50f, 50f, textPaint)
    

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2011-08-20
      • 2016-05-29
      • 1970-01-01
      相关资源
      最近更新 更多