2012-03-27 2 views
2

나는 하나의 이미지를 가지고 있기 때문에 상단과 하단 텍스트를 써야한다. 정적 레이아웃을 사용하여 글을 쓸 수있다. 아래 코드는 다음과 같습니다.안드로이드 : 이미지에 정적 레이아웃에서 최대 두 줄을 제한하는 방법

TextPaint mTextPaintTop= new TextPaint(); 
mTextPaintTop.setColor(Color.RED); 
StaticLayout layoutTop = new StaticLayout(top_text, mTextPaintTop,    
width,Layout.Alignment.ALIGN_CENTER, 1.3f, 0, false); 
canvas.translate(0, 20); //position the text 
layoutTop.draw(canvas); 

텍스트를 최대 두 줄로 제한하고 싶습니다. 사용자가 긴 텍스트를 입력하고 2 줄을 초과하는 경우 글꼴 크기를 줄여 2 줄만 조정할 수 있습니다.

답변

1

다운로드이 파일은 EllipsizingTextView

@Override 
    public void draw(Canvas canvas) { 
    // TODO Auto-generated method stub 

    Paint mpaint = new Paint(Paint.ANTI_ALIAS_FLAG); 

    mpaint.setColor(Color.RED); 

    mpaint.setTextSize(13); 

    mpaint.setTextAlign(Align.CENTER); 

     drawTextOnCanvas(canvas, "text you want 2 line bigger", 0, 20, mpaint); 

    } 

} 

private void drawTextOnCanvas(Canvas canvas, String text, int x, int y, Paint mPaint) { 
     // Setup a textview like you normally would with your activity context 
     EllipsizingTextView tv = new EllipsizingTextView(mContext); 

     tv.setGravity(Gravity.CENTER_HORIZONTAL); 
     tv.setEllipsize(TruncateAt.END); 
     tv.setMaxLines(2); 
     // setup text 
     tv.setText(text); 

     // maybe set textcolor 
     tv.setTextColor(Color.WHITE); 

     // you have to enable setDrawingCacheEnabled, or the getDrawingCache will return null 
     tv.setDrawingCacheEnabled(true); 

     // we need to setup how big the view should be..which is exactly as big as the canvas 
     tv.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY)); 

     // assign the layout values to the textview 
     tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight()); 

     // draw the bitmap from the drawingcache to the canvas 
     canvas.drawBitmap(tv.getDrawingCache(), x, y, mPaint); 

     // disable drawing cache 
     tv.setDrawingCacheEnabled(false); 
    } 
+0

두 개의 라인으로 좁혀왔다. – moDev

+3

draw()의 페인트 및 뷰 객체 생성은 정말 끔찍한 생각입니다. – Teovald

관련 문제