2012-08-30 6 views
4

사용자가 텍스트를 삽입하는 앱이 있는데, 버튼을 클릭하면 미리 지정된 이미지에 해당 텍스트가있는 새로운 이미지가 생성되어 전화기에 저장됩니다.이미지에 텍스트를 페인팅 할 때 새 라인

하지만 텍스트가 너무 길어서 이미지의 너비를 초과하는 경우가 있으므로 새 줄로 나누는 것이 좋습니다. 어떻게해야합니까?

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

그러나 그것은 작동하지 않았다 : 내가 사용하던 ...

나는 BREAKTEXT와 시도,하지만 난 그것을 사용하는 방법을 모르겠어요. 내 코드 원래 코드 : I 수동 글고에서 선을 깰 때

또한

, 그것은 ...에 "[]"여기서 두 번째 줄은 시작해야 하나와 함께

EDIT 모든 것을 보여줍니다

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

답변

0

해결책을 찾았습니다. 레이아웃을 작성하고 문자열을 텍스트 뷰로, 이미지를 배경으로 설정하고 그립니다.

+0

레이아웃 내용을 캔버스에 그려 넣은 방법을 안내해 주시겠습니까? 나는 같은 문제를 겪고있다. – AndyN

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

. 난 당신이 내가 설명 된대로 실제로 문자열을 분할 어디 코드에서 보지 않는 does not seem to support line breaks. So you'll need to draw each line separately with different Y-Values. (Code adapted from here):

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

편집Canvas.drawText를 사용하고 있으리라 믿고있어. 실제로 구현 방법을 보여주지 않으면 내 솔루션이 작동하지 않는 이유를 진단 할 수 없습니다.

여기에서 작업하여 오류 수정 방법을 보여줄 수 있다고 생각합니다. 여러 번 해보고 싶다면 그 방법을 작성하는 것이 좋습니다. 당신의 클래스에 다음 메서드를 추가 :이 코드

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

:

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

이 코드를 교체

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(); 
+0

예, drawText를 사용하고 있습니다. 그것은 줄 바꿈을 지원하지 않는다는 것을 몰랐다. 너무 나쁘다 ... 나는이 코드를 내 프로젝트에 적용하려고 노력하고 있지만 제대로 작동하지 않는다. 한 줄로 이미지를 얻는다. –

+0

질문에 문자열을 그리는 코드를 추가 할 수 있습니까? – Fr33dan

+0

내가 그렸던 원래 코드로 편집했습니다. –

0

근무 용LINK을 참조하십시오. 희망은 유용합니다.

관련 문제