Cameron McBride
Cameron McBride

Reputation: 6799

Best way to write text on an image?

I am wanting to take a little calendar image and write the date and month on it. This will be displayed many times in a list view, so I am looking for the optimum way to do it.

Below works great, is there a better way:

ImageView image = (ImageView) findViewById(R.id.imageView2);
try {
    // Load the png image into a bitmap
    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.calendar_empty);
    // Have to copy the bitmap so it is mutable
    mBitmap = mBitmap.copy(Bitmap.Config.ARGB_8888, true);
    // Create a canvas from the bitmap to draw on
    Canvas canvas = new Canvas(mBitmap);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setTextSize(12);
    paint.setAntiAlias(true);
    canvas.drawText("Sept", 11, 26, paint);
    paint.setTextSize(18);
    canvas.drawText("29", 14, 42, paint);
    // Display the results on the screen
    image.setImageDrawable(new BitmapDrawable(this.getResources(), mBitmap));
} catch (Exception e) {
    e.printStackTrace ();
}

Upvotes: 1

Views: 485

Answers (1)

Zsombor Erdődy-Nagy
Zsombor Erdődy-Nagy

Reputation: 16914

Why don't you just take the ImageView, and put a TextView on it as an overlay for the title, inside a layout container that enables this - e.g. RelativeLayout or FrameLayout?

I had several projects in which I wrote titles on images this way, there were no problems with this approach.

Upvotes: 2

Related Questions