如何用Java(Android)在图像上写文本

Ben*_*and 4 java png android text image

我可以在iPhone上的Objective-C中执行此操作,但现在我正在寻找相应的Android Java代码.我也可以用普通的Java来做,但我不知道Android特定的类是什么.我想在运行中生成一个PNG图像,该图像在图像中居中.

public void createImage(String word,String outputFilePath){/*我该怎么办?*/}

相关主题:

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

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

GET*_*Tah 18

怎么样的:

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);
Run Code Online (Sandbox Code Playgroud)


Jef*_*man 7

您不需要使用图形.

一种更简单的方法是创建一个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>
Run Code Online (Sandbox Code Playgroud)

当然,图像顶部的东西不需要是简单的TextView,它可以是另一个图像,或者包含你喜欢的任意元素的另一个布局.