user1023177
user1023177

Reputation: 641

How to programmatically create simple image with text inside?

I need to create simple image in my application programmatically. Simple image will have black background with text inside which is created programmatically. Is it possible?

Upvotes: 14

Views: 14467

Answers (2)

NikoR
NikoR

Reputation: 548

    int width = 200;
    int height = 100;
    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    Paint paint = new Paint();
    paint.setColor(Color.BLACK); 
    paint.setStyle(Paint.Style.FILL);       
    canvas.drawPaint(paint);

    paint.setColor(Color.WHITE);
    paint.setAntiAlias(true);
    paint.setTextSize(14.f);
    paint.setTextAlign(Paint.Align.CENTER);
    canvas.drawText("Hello Android!", (width / 2.f) , (height / 2.f), paint);

And then do whatever you wanted to do with the Bitmap. For example:

ImageView image = new ImageView();
image.setImageBitmap(bitmap);

Upvotes: 30

Deco
Deco

Reputation: 5159

This depends very much on your implementation details (Java SE? Android? Restricted imports? etc)

I suggest you take a look at this StackOverflow question and see if any of the libraries linked are right for your situation.

Upvotes: 0

Related Questions