Frage

Gibt es eine Möglichkeit "Loading" -Bildschirm zu zeigen mit Animation in Brombeere?

Optionen:

  • PME Animation Inhalt
  • Multithreading + Satz von Bildern + Timer / Zähler
  • Standardfelge api
  • eine andere Art und Weise

Jede davon?

Danke!

War es hilfreich?

Lösung

Fermin, Anthony +1. Vielen Dank an alle, du hast mir den Teil der Antwort.
Meine letzte Lösung:

1.Erstellen oder generieren ( kostenlos Ajax Laden gif Generator ) Animation und fügen Sie es zu projizieren.

2.Create ResponseCallback Schnittstelle (siehe Coderholic - Blackberry WebBitmapField ) Thread-Ausführungsergebnis zu erhalten :

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

3.Create eine Klasse Ihres Hintergrund-Thread Job zu behandeln. In meinem Fall war es http Anfrage:

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 (ein Hybrid aus Fullscreen und AnimatedGIFField mit ResponseCallback Interface):

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.In Ende, erstellen Startbildschirm HttpConnector.HttpGetStream anrufen und WaitScreen zu zeigen:

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

UPDATE: eine andere Lösung naviina. eu: A Web2.0 / Ajax-Stil Laden Popup in einer nativen Blackberry-Anwendung

Andere Tipps

Das Grundmuster für diese Art der Sache ist:

Haben Sie einen Thread eine Schleife ausgeführt, die eine Variable aktualisiert (wie der Rahmenindex des animierten Bildes) und dann Anrufe ungültig auf einem Feld, das das Bild zieht (und schläft dann für einen bestimmten Zeitraum). Die Ungültigkeit wird ein Neuzeichnen des Feldes Warteschlange.

In der paint-Methode Feld, die Variable lesen und den entsprechenden Rahmen des Bildes ziehen.

Pseudo-Code (nicht ganz vollständig, aber Ihnen die Idee geben):

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

Hinweis auch hier habe ich eine Reihe von Bitmaps, aber EncodedImage können Sie ein animiertes GIF als ein Objekt behandeln und beinhaltet Methoden bestimmten Frames zu erhalten.

EDIT: Der Vollständigkeit halber: Fügen Sie diese auf einem PopupScreen (wie in Fermin Antwort) oder einen eigenen Dialog erstellen direkt durch zwingende Bildschirm. Die separaten Thread ist notwendig, weil die RIM-API ist nicht Thread-sicher: Sie alles UI bezogen auf das Ereignis Thread tun müssen (oder während der Veranstaltung Sperre hält, finden Sie unter Blackberry UI Threading - die Grundlagen

Dies ist einfach Code für Ladebildschirm ....

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

Wenn es nur eine Animation kann man zeigt ein animierte gIF auf einem Pop-up und es schließen, wenn der Betrieb Laden abgeschlossen ist?

Der einfachste Weg ist wahrscheinlich die Standard-GaugeField zu verwenden, Stil GaugeField.PERCENT Einstellung. Dadurch erhalten Sie einen Fortschrittsbalken. Fügen Sie dieses einem PopupScreen und es wird auf Ihre Inhalte sitzen. So etwas wie ..

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

Dann Update-Methode haben, die _gaugeField.setValue (newValue) verwendet; die Fortschrittsanzeige zu aktualisieren.

ich normalerweise diese von welcher genannt habe Thread die Arbeit (Laden in Ihrem Fall tut, jedes Mal wenn ein Vorgang abgeschlossen ist der Fortschrittsbalken aktualisiert wird.

Ich würde vorschlagen, einen Blick auf diese einfache Implementierung zu nehmen. Ich mochte diese aber nie benutzt. sein kann hilfreich für Sie.

Link-Text

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top