我們如何解決heap size不夠的事情呢?手機是不能夠轉變其heap size的,我們只有想措施把持heapmemory的應用。最直觀的做法就是:存儲內存與運算內存的優化應用,當運算內存需要資源時從存儲內存中調用,需要新的資源時,就把不需要的開釋掉。下面我就聯合一段代碼說明我們是如何制作Loading狀態的。
眾所周知,Java是內置多線程的,我們可以應用兩個線程來解決loading的標題,一個讀資源的線程,一個繪制資源的線程。程序代碼:
import Javax.microedition.midlet.*;import Javax.microedition.lcdui.*; /** * Loading
Thread innerThread = null; public Loading() { innerThread = new Thread(this); innerThread.start(); } int counter = 100; public void run() { //模仿讀取資源 //把下面的東西改成讀取資源的代碼即可 while (counter > 0) { counter--; try { Thread.sleep(20); } catch (Exception ex) {} } //loading結束 isLoaded = true; } } Loading loading = null; public MainCanvas() { loading = new Loading(); thread = new Thread(this); thread.start(); } int loadingCounter = 0; //繪制.. public void paint(Graphics g) { g.setColor(0); g.fillRect(0, 0, getWidth(), getHeight()); switch (state) { case LOADING: { g.setColor(0XFFFFFF); g.drawString("LOADING" + ">>>>>".substring(0, loadingCounter), getWidth() >> 1, getHeight() >> 1, Graphics.HCENTER | Graphics.TOP); loadingCounter = ++loadingCounter % 5; } break; case GAMEING: { g.setColor(0XFFFFFF); g.drawString("GAME", getWidth() >> 1, getHeight() >> 1, Graphics.HCENTER | Graphics.TOP); } break; } } public void run() { while (true) { try { Thread.sleep(100); } catch (Exception ex) { } if (isLoaded) { loading = null; state = GAMEING; } repaint(0, 0, getWidth(), getHeight()); serviceRepaints(); } }} public class Main extends MIDlet { MainCanvas mc; public void startApp() { if (mc == null) { mc = new MainCanvas(); Display disp = Display.getDisplay(this); disp.setCurrent(mc); } } public void destroyApp(boolean bool) {} public void pauseApp() {}}