Generate image with text

Asked

Viewed 555 times

0

I would like to generate images with dynamic texts!

public void generate()
{

    Bitmap src = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);  
    Bitmap dest = Bitmap.createBitmap(500, 800, Bitmap.Config.ARGB_8888);

    String yourText = "Este é meu texto personalizado que vou adicionar em uma imagem de teste qwerty yrrwwq";

    Canvas cs = new Canvas(dest);
    Paint tPaint = new Paint();
    tPaint.setTextSize(35);
    tPaint.setColor(Color.BLUE);
    tPaint.setStyle(Paint.Style.FILL);
    cs.drawColor(Color.WHITE);
    cs.drawBitmap(src, 0f, 0f, null);

    cs.drawText(yourText, 10, 50, tPaint);
    try {
        dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/ImageAfterAddingText.jpg")));

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
} 

Resultado:

I wonder how do I break the text?

It will be necessary to calculate how many characters fit, and adding each line?

Or is there some way to break automatically?

Thanks in advance!

  • Hello, why you need the text to be part of the image ? why not just put the text on the image ?

  • it will be exported! It is not a screen, but a result that will be generated for the user to export

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.