4

我有一个用户插入文本的应用程序,当他单击一个按钮时,它会在预定图像中生成一个带有该文本的新图像并将其保存在手机上。

但有时该文本太长并超过图像的宽度,所以我想要做的就是将它分成一个新行。我该怎么做?

我尝试使用breakText,但我不确定如何使用它......我正在使用:

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

但它没有用。

此外,当我在 EditText 处手动换行时,它只显示所有内容,并在第二行应开始的位置显示“[]”...

编辑:我的代码原始代码:

    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);
}
4

2 回答 2

3

BreatText返回字符串中在被截断之前可以显示的字符数。我建议循环调用它。删除它可以容纳的许多字符并将它们放置在每次迭代的字符串中,直到源文本为空。:

ArrayList<String> lines = new ArrayList<String>();
String test = text[2];
while(!test.isEmpty()){
    int newLength = textPaint.breakText(test, true, bmp.getWidth(), null);
    lines.add(test.substring(0, newLength));
    test = test.substring(newLength);
}

至于打印多行。我假设您使用的Canvas.drawText似乎不支持换行符。因此,您需要使用不同的 Y 值分别绘制每条线。(代码改编自这里):

Rect bounds = new Rect();
int yoff = 0;
for(String line:lines){
    canvas.drawText(line, x, y + yoff, paint);
    textPaint.getTextBounds(line, 0, line.length(), bounds);
    yoff += bounds.height();
}

编辑我没有在您的代码中看到您实际拆分字符串的位置,正如我所描述的那样。如果您实际上没有向我展示您是如何实现它的,我无法诊断为什么我的解决方案不起作用。

虽然我想我可以从这里开始工作,但我可以向您展示如何修复错误。如果您想多次执行此操作,最好为它编写一个方法。将以下方法添加到您的类中:

public void splitAndDrawLines(Canvas canvas,String text, int x, int y, Paint textPaint, int width){
    ArrayList<String> lines = new ArrayList<String>();
    String test = text;
    while(!test.isEmpty()){
        int newLength = textPaint.breakText(test, true, canvas.getWidth(), null);
        lines.add(test.substring(0, newLength));
        test = test.substring(newLength);
    }
    Rect bounds = new Rect();
    int yoff = 0;
    for(String line:lines){
        canvas.drawText(line, x, y + yoff, textPaint);
        textPaint.getTextBounds(line, 0, line.length(), bounds);
        yoff += bounds.height();
    }
}

替换此代码:

canvas.drawText(text[1], largura, altura2, textPaint);
canvas.drawText(text[2], largura, altura, textPaint);

使用此代码:

this.splitAndDrawLines(canvas, text[1], largura, altura2, textPaint);
this.splitAndDrawLines(canvas, text[2], largura, altura, textPaint);

编辑2:

这是我用来设置和发送代码的代码:

    // Create a 100x100 bitmap
    bmp = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    // Set the height of the text to 12.
    this.tamanho = 12f;
    // Draw the text in the middle of the picture width-wise.
    this.largura = bmp.getWidth() / 2;
    // Text parameters
    this.text = new String[]{"MAKE THE TEXT WHITE", "This text starts in the middle of the middle is too long and will be split","Short text at the top of the image"}; 
    // Start one line size into the picture height-wise.
    this.altura = this.tamanho;
    // Start in the middle of the picture height-wise.
    this.altura2 = bmp.getHeight()/2;
    // Output File name.
    this.fname = "TEST.jpg";
    // Save the image
    SaveMyImage();
于 2012-08-30T03:20:12.680 回答
0

我找到了一个解决方案,我创建了一个布局并将字符串设置为 textview 并将图像设置为背景并绘制它。

于 2012-09-05T23:51:10.793 回答