调用drawtext()时如何自定义换行符?

时间:2012-07-18 08:58:07

标签: blackberry newline drawtext

这是一个列表字段。

public class Custom_ListField extends ListField {
private String[] title, category, date, imagepath;
private int[] newsid, catsid;
private List_News newslist;
private Bitmap imagebitmap[], localimage = Bitmap
        .getBitmapResource("image_base.png");
private BrowserField webpage;
private Custom_BrowserFieldListener listener;
private boolean islatest;

private Vector content = null;
private ListCallback callback = null;

private int currentPosition = 0;

public Custom_ListField(Vector content, boolean islatest) {
    this.content = content;
    this.islatest = islatest;
    newsid = new int[content.size()];
    title = new String[content.size()];
    category = new String[content.size()];
    date = new String[content.size()];
    imagepath = new String[content.size()];
    catsid = new int[content.size()];
    imagebitmap = new Bitmap[content.size()];

    for (int i = 0; i < content.size(); i++) {
        newslist = (List_News) content.elementAt(i);
        newsid[i] = newslist.getID();
        title[i] = newslist.getNtitle();
        category[i] = newslist.getNewCatName();
        date[i] = newslist.getNArticalD();
        imagepath[i] = newslist.getImagePath();

        if (!imagepath[i].toString().equals("no picture")) {
            imagebitmap[i] = Util_ImageLoader.loadImage(imagepath[i]);
        } else {
            imagebitmap[i] = localimage;
        }
        catsid[i] = newslist.getCatID();
    }

    initCallbackListening();
    this.setRowHeight(localimage.getHeight() + 10);
}

private void initCallbackListening() {
    callback = new ListCallback();
    this.setCallback(callback);
}

private class ListCallback implements ListFieldCallback {

    public ListCallback() {
        setBackground(Config_GlobalFunction
                .loadbackground("background.png"));
    }

    public void drawListRow(ListField listField, Graphics graphics,
            int index, int y, int width) {
        currentPosition = index;
        graphics.drawBitmap(
                Display.getWidth() - imagebitmap[index].getWidth() - 5,
                y + 3, imagebitmap[index].getWidth(),
                imagebitmap[index].getHeight(), imagebitmap[index], 0, 0);
        graphics.setColor(Color.WHITE);
        graphics.drawRect(0, y, width, imagebitmap[index].getHeight() + 10);



        graphics.setColor(Color.BLACK);
        graphics.setFont(Font.getDefault().derive(Font.BOLD, 20));
        graphics.drawText(title[index], 5, y + 3, 0, Display.getWidth()
                - imagebitmap[index].getWidth() - 10);

        graphics.setColor(Color.GRAY);
        graphics.setFont(Font.getDefault().derive(Font.BOLD, 15));
        graphics.drawText(date[index], 5, y + 6
                + Font.getDefault().getHeight() + 3);

        if (islatest) {
            graphics.setColor(Color.RED);
            graphics.setFont(Font.getDefault().derive(Font.BOLD, 15));
            graphics.drawText(category[index], Font.getDefault()
                    .getAdvance(date[index]) + 3, y + 6
                    + Font.getDefault().getHeight() + 3);
        }
    }

    public Object get(ListField listField, int index) {
        return content.elementAt(index);
    }

    public int getPreferredWidth(ListField listField) {
        return Display.getWidth();
    }

    public int indexOfList(ListField listField, String prefix, int start) {
        return content.indexOf(prefix, start);
    }
}

public int getCurrentPosition() {
    return currentPosition;
}

protected boolean navigationClick(int status, int time) {
    int index = getCurrentPosition();
    if (catsid[index] == 9) {
        if (Config_GlobalFunction.isConnected()) {
            webpage = new BrowserField();
            listener = new Custom_BrowserFieldListener();
            webpage.addListener(listener);

            MainScreen aboutus = new Menu_Aboutus();
            aboutus.add(webpage);
            Main.getUiApplication().pushScreen(aboutus);

            webpage.requestContent("http://www.orientaldaily.com.my/index.php?option=com_k2&view=item&id="
                    + newsid[index] + ":&Itemid=223");
        } else
            Config_GlobalFunction.Message(Config_GlobalFunction.nowifi, 1);
    } else
        Main.getUiApplication().pushScreen(
                new Main_NewsDetail(newsid[index]));
    return true;
}
}

请查看

graphics.setColor(Color.BLACK);
graphics.setFont(Font.getDefault().derive(Font.BOLD, 20));
graphics.drawText(title[index], 5, y + 3, 0, Display.getWidth()
                - imagebitmap[index].getWidth() - 10);

这只会将文本绘制一行。我做了研究,发现没有内置函数,必须自定义一个函数使文本自动下一行。

像这样的功能

private int numberoflines(int availablespace){
    ...
    return numberlines
}

1 个答案:

答案 0 :(得分:0)

Rupak显示的链接很好,虽然其中一个引用了通用Java问题(并提出了需要为BlackBerry更改的Swing结果),另一个引用了外部(非堆栈溢出)链接。

如果您想要其他选项,并且不希望算法找出 where 来换行,则可以使用此选项。此代码假定您将'\ n'个字符放入字符串中,您希望将文本拆分为多行。您可能会将此代码放在paint()方法中:

     // store original color, to reset it at the end
     int oldColor = graphics.getColor();

     graphics.setColor(Color.BLACK);
     graphics.setFont(_fieldFont);
     int endOfLine = _text.indexOf('\n'); 
     if (endOfLine < 0) {
        graphics.drawText(_text, _padding, _top);
     } else {
        // this is a multi-line label
        int top = _top;
        int index = 0;
        int textLength = _text.length();
        while (index < textLength) {
           // draw one line at a time
           graphics.drawText(_text,
                 index,             // offset into _text
                 endOfLine - index, // number of chars to draw
                 _padding,          // x
                 top,               // y
                 (int) (DrawStyle.HCENTER | DrawStyle.TOP | Field.USE_ALL_WIDTH), // style flags
                 _fieldWidth - 2 * _padding);                                     // width available

           index = endOfLine + 1;
           endOfLine = _text.indexOf('\n', index); 
           if (endOfLine < 0) {
               endOfLine = textLength;
           }
           top += _fieldFont.getHeight() + _top;  // top padding is set equal to spacing between lines
        }
     }

     graphics.setColor(oldColor);

在这里,您将初始化我在其中使用的一些变量。根据您发布的代码,我认为这些是正确的,但您需要仔细检查:

     String _text = title[index];   // text to draw
     int _padding = 5;              // left and right side padding around text
     int _top = y + 3;              // the y coordinate of the top of the text

     Font _fieldFont = Font.getDefault().derive(Font.BOLD, 20);

     // the total width reserved for the text, which includes room for _padding:
     int _fieldWidth = Display.getWidth() - imagebitmap[index].getWidth();