黑莓 - 带有动画的加载/等待界面

13

有没有一种方法在黑莓设备中显示带动画的“正在加载”屏幕?

选项:

  • PME动画内容
  • 多线程+图像集+计时器/计数器
  • 标准RIM API
  • 其他方法

以上任何一种可行吗?

谢谢!


1
您可以在此处使用弹出屏幕查看示例。http://supportforums.blackberry.com/t5/Java-Development/Sample-quot-Please-Wait-quot-screen-part-1/ta-p/493808 我用这个方法解决了我的问题。 - BSKANIA
7个回答

35

Fermin, Anthony +1。感谢大家,你们给了我答案的一部分。
我的最终解决方案:

1.创建或生成(免费Ajax加载gif生成器)动画并将其添加到项目中。

2.创建ResponseCallback接口(参见Coderholic-黑莓WebBitmapField)以接收线程执行结果:

public interface ResponseCallback {
    public void callback(String data);  
}

3.创建一个类来处理后台线程任务。在我的情况下,它是http请求:

public class HttpConnector 
{
  static public void HttpGetStream(final String fileToGet,
    final ResponseCallback msgs) {
    Thread t = new Thread(new Runnable() {
      public void run() {
        HttpConnection hc = null;
    DataInputStream din = null;
    try {
      hc = (HttpConnection) Connector.open("http://" + fileToGet);
      hc.setRequestMethod(HttpsConnection.GET);
      din = hc.openDataInputStream();
      ByteVector bv = new ByteVector();
      int i = din.read();
      while (-1 != i) {
        bv.addElement((byte) i);
        i = din.read();
      }
      final String response = new String(bv.toArray(), "UTF-8");
      UiApplication.getUiApplication().invokeLater(
        new Runnable() {
          public void run() {
        msgs.callback(response);
              }
            });
    } 
        catch (final Exception e) {
          UiApplication.getUiApplication().invokeLater(
            new Runnable() {
              public void run() {
                msgs.callback("Exception (" + e.getClass() + "): " 
                  + e.getMessage());
              }
            });
        } 
        finally {
          try {
            din.close();
            din = null;
            hc.close();
            hc = null;
          }
          catch (Exception e) {
          }
        }
      }
    });
  t.start();
  }
}

4.创建等待屏幕(一个AnimatedGIFField和ResponseCallback接口的混合全屏界面):

public class WaitScreen extends FullScreen implements ResponseCallback 
{
    StartScreen startScreen;
    private GIFEncodedImage _image;
    private int _currentFrame;
    private int _width, _height, _xPos, _yPos;
    private AnimatorThread _animatorThread;
    public WaitScreen(StartScreen startScreen) {
        super(new VerticalFieldManager(), Field.NON_FOCUSABLE);
        setBackground(
            BackgroundFactory.createSolidTransparentBackground(
                Color.WHITE, 100));
        this.startScreen = startScreen;
        EncodedImage encImg = 
          GIFEncodedImage.getEncodedImageResource("ajax-loader.gif");
        GIFEncodedImage img = (GIFEncodedImage) encImg;

        // Store the image and it's dimensions.
        _image = img;
        _width = img.getWidth();
        _height = img.getHeight();
        _xPos = (Display.getWidth() - _width) >> 1;
        _yPos = (Display.getHeight() - _height) >> 1;
        // Start the animation thread.
        _animatorThread = new AnimatorThread(this);
        _animatorThread.start();
        UiApplication.getUiApplication().pushScreen(this);
    }

    protected void paint(Graphics graphics) {
        super.paint(graphics);
            // Draw the animation frame.
            graphics
              .drawImage(_xPos, _yPos, _image
                .getFrameWidth(_currentFrame), _image
                  .getFrameHeight(_currentFrame), _image,
                _currentFrame, 0, 0);
    }

    protected void onUndisplay() {
        _animatorThread.stop();
    }

    private class AnimatorThread extends Thread {
        private WaitScreen _theField;
        private boolean _keepGoing = true;
        private int _totalFrames, _loopCount, _totalLoops;
        public AnimatorThread(WaitScreen _theScreen) {
            _theField = _theScreen;
            _totalFrames = _image.getFrameCount();
            _totalLoops = _image.getIterations();

        }

        public synchronized void stop() {
            _keepGoing = false;
        }

        public void run() {
            while (_keepGoing) {
                // Invalidate the field so that it is redrawn.
                UiApplication.getUiApplication().invokeAndWait(
                  new Runnable() {
                    public void run() {
                        _theField.invalidate();
                    }
                });
                try {
                  // Sleep for the current frame delay before
                  // the next frame is drawn.
                  sleep(_image.getFrameDelay(_currentFrame) * 10);
                } catch (InterruptedException iex) {
                } // Couldn't sleep.
                // Increment the frame.
                ++_currentFrame;
                if (_currentFrame == _totalFrames) {
                  // Reset back to frame 0 
                  // if we have reached the end.
                  _currentFrame = 0;
                  ++_loopCount;
                  // Check if the animation should continue.
                  if (_loopCount == _totalLoops) {
                    _keepGoing = false;
                  }
                }
            }
        }

    }

    public void callback(String data) {
        startScreen.updateScreen(data);
        UiApplication.getUiApplication().popScreen(this);
    }
}

5.最后,创建启动屏幕来调用HttpConnector.HttpGetStream并显示WaitScreen:

public class StartScreen extends MainScreen
{
    public RichTextField text;
    WaitScreen msgs;
    public StartScreen() {       
        text = new RichTextField();
        this.add(text);
    }

    protected void makeMenu(Menu menu, int instance) {
        menu.add(runWait);
        super.makeMenu(menu, instance);
    }

    MenuItem runWait = new MenuItem("wait", 1, 1) {
        public void run() {
            UiApplication.getUiApplication().invokeLater(
                new Runnable() {
                    public void run() {
                        getFile();
                    }
            });             
        }
    };

    public void getFile() {
        msgs = new WaitScreen(this);
        HttpConnector.HttpGetStream(
            "stackoverflow.com/faq", msgs);                 
    }

    //you should implement this method to use callback data on the screen.
    public void updateScreen(String data)
    {
        text.setText(data);
    }
}

更新:另一种解决方案 naviina.eu:在原生黑莓应用程序中使用Web2.0 / Ajax样式的加载弹出窗口


谢谢分享这个。我也可以通过在paint()方法中添加“graphics.drawText(text, xText, yImage);”来在图像旁边显示文本。要计算图像和文本的坐标,使用“this.getFont().getAdvance(text)”和“this.getFont().getHeight();”。 - bob
我可以添加任何帧数的图片吗?我正在添加一个有12个框架的图片,但它没有正确渲染。它时而出现,时而消失... 不确定问题出在哪里... - varunrao321
@新手 打印或查看调试中的_totalLoops值 - 它是要播放的循环次数。检查您的动画是否具有无限循环计数值,可能为1,因此仅播放一次。 - Maksym Gontar
1
如果在加载GIF图像时遇到NPE(NullPointerEx),请阅读此内容:http://supportforums.blackberry.com/t5/Java-Development/Loading-a-gif-image-using-GIFEncodedImage/m-p/362524#M67850 - Mark Joseph Del Rosario

4
这是一个简单的加载屏幕代码...
                HorizontalFieldManager popHF = new HorizontalFieldManager();
                popHF.add(new CustomLabelField("Pls wait..."));
                final PopupScreen waitScreen = new PopupScreen(popHF);
                new Thread()
                {
                    public void run() 
                    {

                        synchronized (UiApplication.getEventLock()) 
                        {
                            UiApplication.getUiApplication().pushScreen(waitScreen);
                        }
                       //Here Some Network Call 

                       synchronized (UiApplication.getEventLock()) 
                        {
                            UiApplication.getUiApplication().popScreen(waitScreen);
                        }
                     }
                 }.start();

4
这种事情的基本模式是:
有一个线程运行一个循环,更新一个变量(如动画图像的帧索引),然后调用 Field 上的 invalidate 方法来绘制图像(然后休眠一段时间)。invalidate 会排队重绘该 Field。
在 Field 的 paint 方法中,读取变量并绘制图像的适当帧。
伪代码(不完全,但可以让你了解):
public class AnimatedImageField extends Field implements Runnable {

   private int currentFrame;
   private Bitmap[] animationFrames;

   public void run() {
     while(true) {
       currentFrame = (currentFrame + 1) % animationFrames.length;
       invalidate();
       Thread.sleep(100);
      }
    }

   protected void paint(Graphics g) {
      g.drawBitmap(0, 0, imageWidth, imageHeight, animationFrames[currentFrame], 0, 0);
    }
  }

请注意,这里我使用了一个位图数组,但EncodedImage可以让您将动画gif视为一个对象,并包含获取特定帧的方法。
编辑:为了完整起见:将此添加到PopupScreen(如Fermin的答案中所示)或通过直接覆盖Screen创建自己的对话框。单独的线程是必要的,因为RIM API不是线程安全的:您需要在事件线程上执行所有与UI相关的操作(或同时持有事件锁,请参阅BlackBerry UI Threading - The Very Basics)。

3
如果只是一种动画效果,您可以在弹出窗口上展示一个动态gif图像,并在加载操作完成后关闭它。

2

最简单的方法可能是使用标准的GaugeField,将样式设置为GaugeField.PERCENT。这将为您提供一个进度条。将其添加到PopupScreen中,它将位于您的内容上方。类似以下内容:

private GaugeField _gaugeField;
private PopupScreen _popup;

public ProgressBar() {    
    DialogFieldManager manager = new DialogFieldManager();
    _popup = new PopupScreen(manager);
    _gaugeField = new GaugeField(null, 0, 100, 0, GaugeField.PERCENT);    
    manager.addCustomField(_gaugeField);
}

然后有一个更新方法,该方法将使用_gaugeField.setValue(newValue);来更新进度条。

通常情况下,我会从执行工作的任何线程(在您的情况下是加载),每次完成操作时都会更新进度条。


谢谢你的回答,但我不需要一个进度条,而是需要一个"等待"动画对话框。你能建议一些连续的自更新技术吗? - Maksym Gontar

2

我建议看一下这个简单的实现。我喜欢它,但从未使用过。也许对你有帮助。

链接文本


是的,这很棒!但是看看我的答案,它已经在最后面了))) - Maksym Gontar


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接