14

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?

sourav.bh
  • 467
  • 1
  • 7
  • 20
user1023177
  • 641
  • 3
  • 10
  • 18
  • Can you elabore more on the question? For what do you want this image? Why won't/can't you use the built-in android ViewWidgets to do this? – josephus Nov 06 '11 at 08:48
  • duplicate of http://stackoverflow.com/questions/8107118/is-it-possible-to-create-image-programatically-on-java-android – Suragch May 10 '15 at 04:17

2 Answers2

27
    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);
NikoR
  • 508
  • 3
  • 7
0

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.

Community
  • 1
  • 1
Deco
  • 5,112
  • 1
  • 16
  • 17
  • Oh, I didn't see the tag, sorry. I'm not very familiar with Android, but I recommend you still take a look at the question I linked; you may be able to user one of the libraries. – Deco Nov 06 '11 at 08:32