2012-12-07 2 views
0

나는 선 그래프를 표시하기 위해 achartengine을 사용하고 있습니다. 내 그래프 제목이 너무 깁니다. 그 때문에 일부 텍스트는 화면을 벗어납니다. 이제는 화면 너비에 맞추기를 원합니다 (여러 줄로 설정할 수 있습니까?). 나는 시도했다. 그러나 나는 얻지 않고있다. 누구든지 나를 도와 줄 수 있어요. 안드로이드 - 어떻게 화면에 그래프 제목을 맞게

는 이미지 enter image description here

답변

0

먼저 참조하여 간단한 방법은 제목 First line\nSecond line에 줄 바꿈을 추가하는 것입니다.

두 번째 방법은 아 차트의 소스를 수정하는 것입니다. AbstractChart 클래스에는 drawString 메서드가 있습니다. 그래프 제목을 그리는 지 아닌지는 모르겠지만 어떻게 완료되는지 알 수 있습니다.

/** 
* Draw a multiple lines string. 
* 
* @param canvas the canvas to paint to 
* @param text the text to be painted 
* @param x the x value of the area to draw to 
* @param y the y value of the area to draw to 
* @param paint the paint to be used for drawing 
*/ 
protected void drawString(Canvas canvas, String text, float x, float y, Paint paint) { 
    String[] lines = text.split("\n"); 
    Rect rect = new Rect(); 
    int yOff = 0; 
    for (int i = 0; i < lines.length; ++i) { 
     canvas.drawText(lines[i], x, y + yOff, paint); 
     paint.getTextBounds(lines[i], 0, lines[i].length(), rect); 
     yOff = yOff + rect.height() + 5; // space between lines is 5 
    } 
} 

필요한 줄 수를 결정해야합니다. 페인트의 measureText(String) 메소드를 사용하여 텍스트 너비를 측정 할 수 있습니다. 그리고 나서 텍스트 너비가 두 줄에있는 사용 가능한 너비보다 큰 경우.

if (paint.measureText(text) > canvas.getWidth()) { 
    ... // Split text in two lines 
     // For example you can do following steps 
     // 1. Find last position of space with `text.lastIndesOf(' ')`. 
     // 2. Then take substring from beginning of text to found last position of space. 
     // 3. Try again with `paint.measureText` if substing fits in available width. 
     // 4. In case it fits - insert line break instead of space, if not start again from 1. (find location of pre-last space, get substring from start to found location, check if it fits and so on...) 
} 
+0

줄 바꿈을 위해 문법적으로 텍스트가 화면을 벗어나는 것을 어떻게 알 수 있습니까? – naresh

+0

'paint.measureText (text)'는 텍스트의 폭을 알려줍니다. 'canvas.getWidth()'는이 텍스트가 그려지는 영역의 넓이를 알려줍니다. 첫 번째가 두 번째보다 큰 경우이 줄을 끊어야한다는 것을 알고 있습니다. – vasart

+0

텍스트가 너비보다 크고 새 줄의 정확한 위치를 어떻게 알 수 있습니까? – naresh

관련 문제