Search code examples
androidcanvaspaint

New line when painting a text in a image


I got an app that the user insert a text, and when he clicks on a button, it generates a new image with that text in a predeterminated image and saves it on the phone.

But sometimes that text is too long and exceeds the image's width, so what I'm trying to do is break it into a new line. How should I do it?

I tried with breakText, but I'm not sure how to use it... I was using:

        textPaint.breakText(text[2], true, bmp.getWidth(), null);

But it didn't work.

Also, when I manually break the line at the EditText it shows everything in only one and with a "[]" where the second line should start...

EDIT: My code original code:

    private void SaveMyImage() {
    // TODO Auto-generated method stub
    File myDir = new File(Environment.getExternalStorageDirectory().getPath()+"/App/");
    myDir.mkdirs();
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
        FileOutputStream out = new FileOutputStream(file);

        Canvas canvas = new Canvas(bmp); 
        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        if (text[0].equals("Image 01")) {
            textPaint.setColor(Color.BLACK);
        }
        else {
            textPaint.setColor(Color.WHITE);
        }
        textPaint.setTextAlign(Align.CENTER);
        textPaint.setTextSize(tamanho);
        textPaint.setShadowLayer(2, 2, 2, Color.BLACK);
        textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern
        canvas.drawBitmap(bmp, 0, 0, null);
        canvas.drawText(text[1], largura, altura2, textPaint);
        canvas.drawText(text[2], largura, altura, textPaint);
        bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
        Toast.makeText(SaveIMG.this, "Image saved on phone", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
       e.printStackTrace();
    }
    sendBroadcast(new Intent(
            Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/App/"+fname);
    pronto.setImageURI(uri);
}

Solution

  • I found a solution, I created a layout and set the string as a textview and the image as a background and drawed it.