How to write text on an image

23,184

Solution 1

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

Solution 2

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.

Solution 3

I think this is also a good solution that can solve your problem. Or download the 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;
    }

The above function draws a bitmap image from a text Or download the source code demo and see how it works

Share:
23,184
Ben Holland
Author by

Ben Holland

Ben Holland is a Senior Research Engineer at Apogee Research. His past work experience has been in research at Iowa State University, mission assurance at MITRE, government systems at Rockwell Collins, and systems engineering at Wabtec Railway Electronics. Ben holds a PhD in Computer Engineering with a focus on secure and reliable computing, a M.S. degree in Computer Engineering and Information Assurance, a B.S. in Computer Engineering, and a B.S. in Computer Science. He was a member of Iowa State University's Knowledge-Centric Software Lab where he taught Software Engineering 421 and worked on DARPA's Automated Program Analysis for Cybersecurity (APAC) and Space/Time Analysis for Cybersecurity (STAC) programs.

Updated on January 15, 2020

Comments