4

ユーザーがテキストを挿入するアプリを入手しました。ユーザーがボタンをクリックすると、そのテキストを使用して新しい画像が事前に決定された画像に生成され、電話に保存されます。

しかし、そのテキストが長すぎて画像の幅を超えることがあるので、私がやろうとしているのは、それを新しい行に分割することです。どうすればいいですか?

breakTextを試してみましたが、使用方法がわかりません...使用していたもの:

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

しかし、それはうまくいきませんでした。

また、EditTextで手動で行を分割すると、すべてが1つだけで表示され、2行目が始まる場所に「[]」が表示されます...

編集:私のコード元のコード:

    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

解決策を見つけ、レイアウトを作成し、文字列をテキストビューとして設定し、画像を背景として設定して描画しました。

于 2012-09-05T23:51:10.793 に答える