⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 taglayout.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            // while odd numbers are Item's content (text or image)            if ((i % 2) == 0) {                obj = item.getLabel();                                // Insert a new line if Label does not start                // on a new line                if (obj != null && !((String)obj).equals("")                     && curWidth != 0) {                    lastIndex = addBreak((short)i, (short)0, lastIndex);                    numLines++;                                        curWidth = 0;                 }            } else {                                  if (item instanceof StringItem) {                    obj = ((StringItem)item).getText();                } else if (item instanceof ImageItem) {                    obj = ((ImageItem)item).getImage();                }            }            if (obj == null) {                continue;            }            // Next object to be laid out is an Image            if (obj instanceof Image) {                Image img      = (Image)obj;                int imgWidth   = img.getWidth();                int imgHeight  = img.getHeight();                int layout     = ((ImageItem)item).getLayout();                // new line before image should be added if image does not                // fit on this line or if LAYOUT_NEWLINE_BEFORE was set                // in Item's layout                boolean addNewLineBefore =                     (((layout & ImageItem.LAYOUT_NEWLINE_BEFORE) != 0) ||                         (curWidth + imgWidth > width));                // new line before image should be added                 // if LAYOUT_NEWLINE_AFTER was set in Item's layout and this                // is not the last image on this layout                boolean addNewLineAfter =                    (((layout & ImageItem.LAYOUT_NEWLINE_AFTER) != 0) &&                      (i < 2*numItems-1));                if (!addNewLineBefore && !addNewLineAfter) {                    curWidth += imgWidth;                }                // add line breaks if new line has to be added before, or                // after, or if image is taller than the lineHeight                if (addNewLineBefore || addNewLineAfter ||                    imgHeight > lineHeight) {                                        int heightOffset = 0;                    if ((addNewLineBefore && curWidth == 0) ||                        !addNewLineBefore) {                        heightOffset = lineHeight;                    }                    for (; heightOffset < imgHeight;                         heightOffset += lineHeight) {                        lastIndex = addBreak((short)i, (short)heightOffset,                                             lastIndex);                        numLines++;                    }                    if (addNewLineAfter) {                        lastIndex = addBreak((short)(i+1), (short)0,                                             lastIndex);                                                numLines++;                        curWidth = 0;                    } else if (addNewLineBefore) {                        curWidth = imgWidth;                    }                }            } else if (obj instanceof String) { // Text                // Next object to be laid out is an Image                String str = (String)obj;                int strLength = str.length();                if (strLength == 0) continue;                int wordWidth  = 0;                int spaceIndex = -1;                /*                int curStart = fromLine == numLines-1 ?                                lineBreaks[lastInfo-1] : 0;                               */                for (int j = 0; j < strLength; j++) {                    char ch = str.charAt(j);                    int charWidth = f.charWidth(ch);                    // add a line break if next character is '\n';                    // if this is the last character in this string                    // then first element on the next line will be                    // with a 0 offset, otherwise it will be next character                    if (ch == '\n') {                        if (j == strLength-1) {                            lastIndex = addBreak((short)(i+1), (short)0,                                                  lastIndex);                        } else {                            lastIndex = addBreak((short)i, (short)(j+1),                                                 lastIndex);                        }                        numLines++;                        curWidth = wordWidth = 0;                        spaceIndex = -1;                                                continue;                    }                                        if (ch == ' ') {                        spaceIndex = j;                        wordWidth = 0;                    } else {                        wordWidth += charWidth;                    }                    curWidth += charWidth;                    // add a line break at the last recoreded word break                    // if next character does not fit on this line                    if (curWidth > width) {                                                 if (wordWidth > width) { // long word                            lastIndex = addBreak((short)i, (short)j,                                                  lastIndex);                            curWidth = wordWidth = charWidth;                        } else {              // do word wrap                            lastIndex = addBreak((short)i,                                                  (short)(spaceIndex+1),                                                 lastIndex);                            if (j == spaceIndex) {                                wordWidth = 0;                            }                            curWidth = wordWidth;                        }                        numLines++;                        spaceIndex = -1;                    }                }            }        }                // always add the last break (for the last line)        addBreak((short)(2*numItems), (short)0, lastIndex);        numLines++;        height = numLines * lineHeight;        if (minHeight != -1 && height < minHeight) {            height = minHeight;        }        if (maxHeight != -1 && height > maxHeight) {            height = maxHeight;        }        // printLines();    }    /**     * Update the content of this layout     *     * @param index The index of the Item to update     * @return int The delta height change caused by the update     */    private int updateContent(int index) {        // update lines array if it was set up already        if (width <= 0) {            return 0;        }        int oldHeight = height;        if (index == 0 || numLines == 0) {            updateLineBreaks(width);            return height - oldHeight;        }        for (int line = 0; line < numLines; line++) {            int nextIndex = line+1 < numLines ?                 lineBreaks[line+1] : numItems;            if (index > nextIndex) {                if (line > 0) line--;            }                        if (index >= nextIndex) {                if (line > 0) line--;                updateLineBreaks(/* line, */width);                return height - oldHeight;            }        }        updateLineBreaks(/* numLines-1, */ width);        return height - oldHeight;    }    /*    void printLines() {                int next = 0;        int nextIndex  = 0;        int nextOffset = 0;        System.err.println("\nPrintlines numLines=" +numLines+                           " lineBreaks.length=" +lineBreaks.length+                            " numItems=" +numItems+                            " items.length=" +items.length);        for (int j = nextIndex, offset = nextOffset; j < 2*numItems; ) {            // move to the next subItem if the end of current item was painted            if (j < nextIndex) {                j++;                if (j >= 2*numItems) {                    break;                }                                offset = 0;            }            // move to next line if the end of current line was reached            if (j == nextIndex && offset == nextOffset) {                if (next > 2*numLines) {                    break;                }                nextIndex = lineBreaks[next++];                nextOffset = lineBreaks[next++];                                System.err.println("Line: " +(next/2-1)+                                    " index=" +(j/2)+                                   " offset=" +offset+                                   " nextIndex=" +nextIndex+                                   " nextOffset=" +nextOffset);            }                        Item item = items[j/2];                        boolean isLabel = ((j%2) == 0);            Object obj = null;            if (isLabel) {                obj = item.getLabel();            } else {                                  if (item instanceof StringItem) {                    obj = ((StringItem)item).getText();                } else if (item instanceof ImageItem) {                    obj = ((ImageItem)item).getImage();                }            }                        if (obj == null) continue;            if (obj instanceof Image) {                Image img = (Image)obj;                int imgHeight = img.getHeight();                int layout = ((ImageItem)item).getLayout();                String layoutStr = "(";                if ((layout & ImageItem.LAYOUT_NEWLINE_BEFORE) != 0) {                    layoutStr += "LAYOUT_NEWLINE_BEFORE ";                }                if ((layout & ImageItem.LAYOUT_NEWLINE_AFTER) != 0) {                    layoutStr += "LAYOUT_NEWLINE_AFTER ";                }                                if ((layout & ImageItem.LAYOUT_DEFAULT) != 0) {                    layoutStr += "LAYOUT_DEFAULT ";                }                if ((layout & ImageItem.LAYOUT_CENTER) != 0) {                    layoutStr += "LAYOUT_CENTER ";                }                if ((layout & ImageItem.LAYOUT_LEFT) != 0) {                    layoutStr += "LAYOUT_LEFT ";                }                if ((layout & ImageItem.LAYOUT_RIGHT) != 0) {                    layoutStr += "LAYOUT_RIGHT ";                }                layoutStr += ")";                System.err.println("\t Image: layout=" +layout+                                   " j=" +j+ " index=" +(j/2)+                                    " offset=" +offset+                                   " layout=" +layoutStr);                                offset += lineHeight;            } else if (obj instanceof String) {                String str = (String)obj;                int strLen = str.length();                                int endIndex = (j == nextIndex ? nextOffset : strLen);                 System.err.print("\t" + (isLabel ? "Label" : "Text"));                System.err.println(" (j=" +j+ " index=" +(j/2)+                                   " offset=" +offset+                                    " endIndex=" +endIndex+")='" +                                   str.substring(offset, endIndex)+                                    "'");                offset = endIndex;            }        }    } */}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -