如何在图像上写文字


问题内容

我可以在iPhone上的Objective-C中执行此操作,但是现在我正在寻找等效的Android
Java代码。我也可以用普通Java做到这一点,但是我不知道Android特定的类是什么。我想动态生成一个PNG图像,该图像中有一些文本居中。

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

相关线程:

  • 如何在Objective-C(iOS)中的图像上写文字?

  • 如何使用Java加载图像并向其中写入文本?


问题答案:

怎么样呢?

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);