lwuit一些技巧

1、textArea 顯示文本內容,在部分手機上無法顯示全部內容,每一行的最後幾個字被擋住
琢磨了很久終於找了出來,解決方案如下:
  TextArea txtContent = new TextArea(strContent, 12, 24);
  //添加這一句即可
  txtContent.setWidestChar('一');
2、若要對文本框中的內容設置補丁:
txtContent.getStyle().setPadding(Component.RIGHT, 10);
內容往右10像素。
3、如果list上不想要顯示文字多餘時的省略號
name.setEndsWith3Points(false);
4、重寫Dialog要讓標題與Form的樣式一致
 

dialog.show(100, 100,100,100, true);
5、聲音播放
try {
     InputStream is = getClass().getResourceAsStream(
       "/res/NewMailSound.wav");
     Player player = Manager.createPlayer(is, "audio/x-wav");
     player.start();
    } catch (Exception e) {
     e.printStackTrace();
    }
6、使得TextField也能夠在觸屏手機上點擊時出現輸入編輯
解決方法:
在TextField源碼上 加上editString();函數:
public void pointerReleased(int x, int y) {
        // unlike text area the text field supports shifting the cursor with the touch screen
     editString();
        String text = getText();
        int textLength = text.length();
        int position = 0;
        Font f = getStyle().getFont();
        x -= getAbsoluteX();
        for(int iter = 0 ; iter < textLength ; iter++) {
            int width = f.substringWidth(text, 0, iter);
            if(x > width) {
                position = iter;
            } else {
                break;
            }
        }
        if(position == textLength - 1) {
            if(f.stringWidth(text) < x) {
                position = textLength;
            }
        }
        setCursorPosition(position);
        repaint();
    }
或者官方的解決方法:http://forums.java.net/jive/thread.jspa?threadID=52716
7、震動
 public void MakeVibrate() {
  new Thread() {
   public void run() {
    try {
     Display.getInstance().vibrate(2000);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  }.start();
 }
8、導致內存激增的原因(可以用自動模擬器的內存檢測器進行監測C:/WTK2.5.2/bin/prefs.exe將你要的設置勾選)
而lwuit裏面的源碼有兩句是會導致內存一直佔用,一個是TextField中的這段代碼
 
Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->    // public boolean animate() {
    // boolean ani = super.animate();
    // if (hasFocus()) {
    // long currentTime = System.currentTimeMillis();
    // if (drawCursor) {
    // if ((currentTime - cursorBlinkTime) > blinkOnTime) {
    // cursorBlinkTime = currentTime;
    // drawCursor = false;
    // return true;
    // }
    // } else {
    // if ((currentTime - cursorBlinkTime) > blinkOffTime) {
    // cursorBlinkTime = currentTime;
    // drawCursor = true;
    // return true;
    // }
    // }
    // if (pressedAndNotReleased) {
    // if (currentTime - pressTime >= getLongClickDuration()) {
    // longClick(pressedKeyCode);
    // }
    // } else {
    // if (pendingCommit && currentTime - releaseTime > commitTimeout) {
    // commitChange();
    // }
    // }
    // } else {
    // drawCursor = false;
    // }
    // return ani;
    // }

一個是Display
lwuitGraphics.setGraphics(impl.getNativeGraphics());
這兩個暫時還沒有仔細去研究,但是確實喫內存的所在。
還有就是要巧用System.gc();進行內存回收,這樣就會盡量的減少內存溢出的情況。
9、滾動條拖拽方向與內容顯示相反,component中的代碼修改如下
 
Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->    public void pointerDragged(int x, int y) {
        if (isScrollable() && isSmoothScrolling()) {
            int axisValue;
            if (isScrollableY()) {
                axisValue = y;
            } else {
                axisValue = x;
            }

            if (!dragActivated) {
                dragActivated = true;
                beforeLastScrollY = axisValue;
                lastScrollY = axisValue;
                getComponentForm().setDraggedComponent(this);
            }
            //save time and locations to create velocity when the
            //pointer is released
            long currentTime = System.currentTimeMillis();
            if (currentTime != lastTime[(pLastDragged + lastTime.length + 1) % lastTime.length]) {
                lastTime[pLastDragged] = System.currentTimeMillis();
                lastDragged[pLastDragged] = axisValue;
                pLastDragged = (++pLastDragged) % lastTime.length;
            }

            beforeLastScrollY = lastScrollY;
            lastScrollY = axisValue;

            // we drag inversly to get a feel of grabbing a physical screen
            // and pulling it in the reverse direction of the drag
            if (isScrollableY()) {
                int scroll = getScrollY() - (beforeLastScrollY - axisValue);
                if (scroll >= 0 && scroll < getPreferredH() - getHeight()) {
                    setScrollY(scroll);
                }
            } else {
                int scroll = getScrollX() - (beforeLastScrollY - axisValue);
                if (scroll >= 0 && scroll < getPreferredW() - getWidth()) {
                    setScrollX(scroll);
                }
            }
        } else {
            //try to find a scrollable element until you reach the Form
            Component parent = getParent();
            if (!(parent instanceof Form)) {
                parent.pointerDragged(x, y);
            }
        }
    }

10、開啓wtk模擬器的觸摸屏功能
打開/wtklib/devices/DefaultColorPhone目錄下的DefaultColorPhone.properties文件(最好先安裝一個UltraEdit之類的文本編輯器)。
然後找到touch_screen選項,修改爲touch_screen=true

11、設置模擬器權限,以免開發過程中彈出煩人的提示
打開wtk模擬器。
選擇Edit->Preferences->Security
然後將Security domain的選項設置爲maximum。

12、內存和性能監視器
Edit->Preferences->Memory Monitor
Edit->Preferences->Profiler

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章