0

線グラフを表示するためにachartengineを使用しています。グラフのタイトルが長すぎます。そのため、一部のテキストは画面を超えています。画面幅に合わせたい(複数行に設定することはできますか?)。試しましたが、うまくいきません。誰かが私を助けることができますか?

画像を参照 ここに画像の説明を入力してください

4

1 に答える 1

0

最初の最も簡単な方法は、タイトルに改行を追加することFirst line\nSecond lineです。

2 番目の方法は、achart のソースを変更することです。クラスにはdrawStringメソッドがあります。AbstractChartグラフのタイトルを描画しているかどうかはわかりませんが、それがどのように行われたかがわかります。

/**
 * 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)ペイントの方法でテキスト幅を測定できます。そして、テキスト幅が使用可能な幅よりも大きい場合、テキストを 2 行に分割します。

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...)
}
于 2012-12-07T11:47:02.220 に答える