سؤال

هل هناك طريقة لإظهار شاشة "جاري التحميل"؟ مع الرسوم المتحركة في بلاك بيري؟

خيارات:

  • محتوى الرسوم المتحركة PME
  • تعدد الخيوط + مجموعة من الصور + مؤقت/عداد
  • حافة API القياسية
  • بطريقة أخرى

أي من هذه؟

شكرًا!

هل كانت مفيدة؟

المحلول

فيرمين، أنتوني +1.شكرا للجميع، لقد أعطيتني جزء من الإجابة.
الحل النهائي الخاص بي:

1. إنشاء أو إنشاء (اياكس تحميل مولد 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. إنشاء WaitScreen (مزيج من FullScreen و الرسوم المتحركة GIFField مع واجهة 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 في تطبيق BlackBerry أصلي

نصائح أخرى

النمط الأساسي لهذا النوع من الأشياء هو:

لديك مؤشر ترابط يقوم بتشغيل حلقة تقوم بتحديث متغير (مثل فهرس الإطار للصورة المتحركة) ثم يتم إبطال الاستدعاءات في الحقل الذي يرسم الصورة (ثم ينام لفترة من الوقت).سوف يقوم الإبطال بوضع قائمة الانتظار لإعادة رسم الحقل.

في طريقة طلاء الحقل، اقرأ المتغير وارسم الإطار المناسب للصورة.

الكود الزائف (ليس مكتملًا تمامًا، ولكن لإعطائك الفكرة):

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) أو قم بإنشاء مربع الحوار الخاص بك عن طريق تجاوز الشاشة مباشرة.يعد الخيط المنفصل ضروريًا لأن واجهة RIM API ليست آمنة لمؤشر الترابط:تحتاج إلى القيام بكل ما يتعلق بواجهة المستخدم في سلسلة الأحداث (أو أثناء الضغط على قفل الحدث، راجع BlackBerry UI Threading - الأساسيات

هذا كود بسيط لتحميل الشاشة ....

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

إذا كانت مجرد رسوم متحركة، هل يمكنك عرضها جيف المتحركة في نافذة منبثقة وإغلاقها عند اكتمال عملية التحميل؟

ربما تكون أسهل طريقة هي استخدام 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);لتحديث شريط التقدم.

عادةً ما يتم استدعاء هذا من أي مؤشر ترابط يقوم بالعمل (التحميل في حالتك، في كل مرة تكتمل العملية، يتم تحديث شريط التقدم.

أود أن أقترح إلقاء نظرة على هذا التنفيذ البسيط.أعجبني هذا ولكن لم أستخدمه أبدًا.قد يكون مفيدا لك.

نص الرابط

يعد ActivityIndicator خيارًا جيدًا إذا كنت تعمل مع BB OS 6.0 على الأقل.

http://www.brighthub.com/mobile/blackberry-platform/articles/94258.aspx

http://docs.blackberry.com/en/developers/deliverables/17966/Screen_APIs_1245069_11.jsp

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top