Pregunta

¿Hay alguna forma de mostrar " Cargando " ¿Pantalla con animación en blackberry?

Opciones:

  • contenido de animación PME
  • multihilo + conjunto de imágenes + temporizador / contador
  • api de llanta estándar
  • de otra manera

¿Algo de esto?

¡Gracias!

¿Fue útil?

Solución

Fermín, Anthony +1. Gracias a todos, me diste la parte de la respuesta.
Mi solución final:

1.Crear o generar ( animación del generador de gif de Ajax gratis ) y agregarla al proyecto.

2.Create la interfaz ResponseCallback (consulta Coderholic - Blackberry WebBitmapField ) para recibir el resultado de la ejecución del hilo :

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

3.Cree una clase para manejar su trabajo de subproceso en segundo plano. En mi caso fue solicitud 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.Create WaitScreen (un híbrido de FullScreen y AnimatedGIFField con interfaz 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. Al final, cree la pantalla de Inicio para llamar a HttpConnector.HttpGetStream y para mostrar 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);
    }
}

ACTUALIZACIÓN: otra solución naviina. eu: una ventana emergente de carga de estilo Web2.0 / Ajax en una aplicación nativa de BlackBerry

Otros consejos

El patrón básico para este tipo de cosas es:

Un hilo que ejecuta un bucle que actualiza una variable (como el índice de cuadros de la imagen animada) y luego llama a invalidar en un campo que dibuja la imagen (y luego duerme durante un período de tiempo). La invalidación pondrá en cola una nueva imagen del campo.

En el método de pintura del campo, lee la variable y dibuja el marco apropiado de la imagen.

Pseudo código (no totalmente completo, pero para darle una idea):

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);
    }
  }

Tenga en cuenta que aquí también utilicé una matriz de mapas de bits, pero EncodedImage le permite tratar un gif animado como un objeto, e incluye métodos para obtener marcos específicos.

EDITAR: Para completar: agregue esto a una pantalla emergente (como en la respuesta de Fermin) o cree su propio cuadro de diálogo anulando la pantalla directamente. El subproceso separado es necesario porque la API de RIM no es segura para subprocesos: debe hacer todo lo relacionado con la IU en el subproceso del evento (o mientras mantiene el bloqueo del evento, consulte BlackBerry UI Threading - Lo más básico

Este es un código simple para cargar la pantalla ....

                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();

Si es solo una animación, podría mostrar un gif animado en una ventana emergente y cerrarla cuando se complete la operación de carga?

La forma más fácil es probablemente usar el GaugeField estándar, que establece el estilo GaugeField.PERCENT. Esto te dará una barra de progreso. Agrega esto a un PopupScreen y se colocará sobre tu contenido. Algo así como ...

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);
}

Luego tenga un método de actualización que usará _gaugeField.setValue (newValue); para actualizar la barra de progreso.

Normalmente recibo esta llamada desde cualquier subproceso que esté haciendo el trabajo (en su caso, cada vez que se completa una operación, se actualiza la barra de progreso.

Sugeriría echar un vistazo a esta implementación simple. Me gustó esto pero nunca lo usé. Puede ser de ayuda para usted.

texto del enlace

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top