7

I can do this in Objective-C on the iPhone, but now I'm looking for the equivalent Android Java code. I can also do it in plain Java, but I don't know what the Android specific classes are. I want to generate a PNG image on the fly that has some text centered in the image.

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

Relevant threads:

Léa Gris
  • 17,497
  • 4
  • 32
  • 41
Ben Holland
  • 2,309
  • 4
  • 34
  • 50

3 Answers3

23

What about something like:

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);
GETah
  • 20,922
  • 7
  • 61
  • 103
7

You don't need to use graphics.

A simpler approach would be to create a FrameLayout with two elements- the ImageView for the image, and another view for whatever you want drawn on top.

<?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>

Of course, the thing on top of the image doesn't need to be a simple TextView, it can be another image, or another layout containing whatever arbitrary elements you like.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • This is easiest way, and works not only with static images but also surface views or whatever else – Konstantin Pribluda Jul 03 '12 at 20:08
  • I'll give you an upvote because I agree with you in most circumstances. But I specifically asked a different question because I have a requirement that it needs to be embedded in the image. – Ben Holland Jul 03 '12 at 20:21
  • 1
    where in your question did you say that it needs to be embedded? anyway. – Jeffrey Blattman Jul 03 '12 at 21:55
  • Good point, I apologize. I thought I did, but I should have been more explicit. – Ben Holland Jul 04 '12 at 16:27
  • It might not have been said explicitly but I did understand it that he wants the text embedded. You can also see that from the two provided links as well. So although he was nice to give you upvote because he is afraid of confrontation he should not have. – h3dkandi May 28 '19 at 06:14
  • Hi friend. This answer wasn't accepted, but clearly people find it useful. Sorry this offends you enough to spend your karma downvoting it. – Jeffrey Blattman May 28 '19 at 18:04
0

GETah's answer wasn't working for me so I tweeked it a bit, here's a Kotlin version, which works, using 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)
Skoua
  • 3,373
  • 3
  • 38
  • 51