创建自定义加载屏幕

时间:2012-05-18 08:00:51

标签: blackberry load loading illegalstateexception

我想创建一个自定义加载屏幕 我已经创建了一个类,但它不能正常工作

我创建了一个LoadingScreen类,它传递一组图像String以在加载时加载图像

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;


public class LoadingScreen extends MainScreen {
    VerticalFieldManager vfm_Progress;
    String progressImgStr[]={"progress0000.png","progress0001.png","progress0002.png","progress0003.png","progress0004.png","progress0005.png","progress0006.png","progress0007.png"};

    public LoadingScreen() {
    this.getMainManager().setBackground(BackgroundFactory.createSolidTransparentBackground(Color.BLACK, 200));
    vfm_Progress=new VerticalFieldManager();
    LoadThread spin=new LoadThread(progressImgStr,this);
    spin.startLoading();
    spin.start();

    }       

}

然后,该类调用一个Thread类,该类加载图像并附加到LoadingScreen的VerticalFieldManager 但是......它没有工作,因为我得到一个名为IIlegalStateException的错误

Thread类如下

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.component.BitmapField;


public class LoadThread extends Thread{
    private String[] mProgressImgStr;
    private boolean isLoading=false;
    LoadingScreen loadScreen;
    public LoadThread(String[] progressImgStr,LoadingScreen loadScreen) {
        this.mProgressImgStr=progressImgStr;
        this.loadScreen=loadScreen;
    }

    public void run() {
        if(isLoading)
        {
            for (int i = 0; i < mProgressImgStr.length; i++) {
                Bitmap img=Bitmap.getBitmapResource(mProgressImgStr[i]);
                BitmapField progressImgFld=new BitmapField(img);
                loadScreen.vfm_Progress.add(progressImgFld);
                loadScreen.add(loadScreen.vfm_Progress);
                try {
                    this.sleep(250);
                    loadScreen.vfm_Progress.delete(progressImgFld);
                    loadScreen.delete(loadScreen.vfm_Progress);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if(i==mProgressImgStr.length-1)
                {
                    i=0;
                }
            }
        }


        //super.run();
    }

    public void stopLoading()
    {
        isLoading=false;
    }

    public void startLoading()
    {
        isLoading=true;
    }
}

2 个答案:

答案 0 :(得分:1)

LoaderScreen.java

import java.util.Timer;
import java.util.TimerTask;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class LoaderScreen extends PopupScreen
{
    Timer loadingTimer = new Timer();
    TimerTask loadingTask;
    int imageIndex = 0;
    Bitmap loader1 = Bitmap.getBitmapResource("loader001.png");
    Bitmap loader2 = Bitmap.getBitmapResource("loader002.png");
    Bitmap loader3 = Bitmap.getBitmapResource("loader003.png");
    Bitmap loader4 = Bitmap.getBitmapResource("loader004.png");
    Bitmap loader5 = Bitmap.getBitmapResource("loader005.png");
    Bitmap loader6 = Bitmap.getBitmapResource("loader006.png");
    Bitmap loader7 = Bitmap.getBitmapResource("loader007.png");
    Bitmap loader8 = Bitmap.getBitmapResource("loader008.png");
    Bitmap loader9 = Bitmap.getBitmapResource("loader009.png");
    Bitmap loader10 = Bitmap.getBitmapResource("loader010.png");
    Bitmap loader11 = Bitmap.getBitmapResource("loader011.png");
    Bitmap loader12 = Bitmap.getBitmapResource("loader012.png");
    boolean showLoader = true;
    int screenWidth = Display.getWidth();
    int screenHeight = Display.getHeight();
    int xAnchor = 0; // (screenWidth-200)/2;
    int yAnchor = 0; // (screenHeight-40)/2;
    String loaderText = "Please wait...";

    public LoaderScreen() 
    {
        super(new VerticalFieldManager(), USE_ALL_HEIGHT | USE_ALL_WIDTH | DEFAULT_CLOSE);
        loadingTask = new TimerTask() {
            public void run() {
                invalidate();
                imageIndex++;
                if(imageIndex == 11){
                    imageIndex = 0;
                }
            }
        };
        loadingTimer.scheduleAtFixedRate(loadingTask, 0, 100);
    }

    public void setShowLoader(boolean showLoader) {
        this.showLoader = showLoader;
    }

    public boolean isShowing(){
        return showLoader;
    }

    protected void paint(Graphics graphics) {
        super.paint(graphics);
        if (showLoader) {
            //draw background:
            graphics.setColor(0x363636);
            graphics.fillRect(xAnchor, yAnchor, 200, 40);

            //draw animation frame:
            switch (imageIndex) {
                case 0:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader1, 0, 0);
                    break;
                case 1:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader2, 0, 0);
                    break;
                case 2:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader3, 0, 0);
                    break;
                case 3:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader4, 0, 0);
                    break;
                case 4:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader5, 0, 0);
                    break;
                case 5:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader6, 0, 0);
                    break;
                case 6:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader7, 0, 0);
                    break;
                case 7:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader8, 0, 0);
                    break;
                case 8:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader9, 0, 0);
                    break;
                case 9:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader10, 0, 0);
                    break;
                case 10:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader11, 0, 0);
                    break;
                case 11:
                    graphics.drawBitmap(xAnchor + 4, yAnchor + 4, 32, 32, loader12, 0, 0);
                    break;
            }

            //draw text:
            graphics.setColor(0xefefef);
            graphics.drawText(loaderText, xAnchor + 44, yAnchor + 6);

            //draw border:
            graphics.setColor(0xcccccc);
            graphics.drawRect(xAnchor, yAnchor, 200, 40);
        }
    }

    protected void sublayout(int maxWidth, int maxHeight) {
        maxWidth = 200;
        maxHeight = 40;
        super.sublayout(maxWidth, maxHeight);
        setExtent(maxWidth, maxHeight);
        int x = (Display.getWidth() >> 1) - (maxWidth >> 1) - 15;
        int y = (Display.getHeight() >> 1) - (maxHeight >> 1) - 30 ;
        setPosition(x, y);
    }

    public void close() 
    {
        try
        {
            loadingTimer.cancel();
            loadingTask.cancel();
        }catch (Exception e) {  }
        super.close();
    }

}

此处您可以将此类与下面的代码一起使用。

LoaderScreen loaderScreen = new LoaderScreen();
UiApplication.getUiApplication().invokeLater(new Runnable() {
    public void run() {
        UiApplication.getUiApplication().pushModalScreen(
                loaderScreen);
    }
});


Thread thread = new Thread() {
    public void run() {
        //Do Something 
        UiApplication.getUiApplication().invokeLater(
            new Runnable() {
                public void run() {
                    try {
                            loaderScreen.close();
                        } catch (Exception e) {
                    }
                }
        });
    }
};
thread.start();

您需要自己查找图片。

答案 1 :(得分:0)

更改UI更新代码如下:

synchronized (UiApplication.getEventLock()) {
    loadScreen.vfm_Progress.add(progressImgFld);
    loadScreen.add(loadScreen.vfm_Progress);
    try {
        this.sleep(250);
        loadScreen.vfm_Progress.delete(progressImgFld);
        loadScreen.delete(loadScreen.vfm_Progress);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

涉及设备用户界面的任何操作都必须使用锁定UiApplication.getEventLock()来完成。