ブラックベリー で「読み込み中」画面をアニメーションで表示する方法はありますか?
オプション:
- PME アニメーション コンテンツ
- マルチスレッド + 画像セット + タイマー/カウンター
- 標準リム API
- 他の方法で
これのどれ?
ありがとう!
ブラックベリー で「読み込み中」画面をアニメーションで表示する方法はありますか?
オプション:
これのどれ?
ありがとう!
フェルミン、アンソニー+1。みんなのおかげで、あなたは私に答えの一部をくれました。
私の最終的な解決策:
1.アニメーションを 作成または生成し(無料のAjaxローディングgifジェネレーター)、プロジェクトに追加します。
2.スレッド実行結果を受信するため のResponseCallbackインターフェイス(Coderholic-Blackberry 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と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:ネイティブBlackBerryアプリケーションでのWeb2.0/Ajaxスタイルの読み込みポップアップ
この種の基本的なパターンは次のとおりです。
変数 (アニメーション画像のフレーム インデックスなど) を更新するループをスレッドで実行し、次に画像を描画する 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 を 1 つのオブジェクトとして扱うことができ、特定のフレームを取得するメソッドが含まれています。
編集:完全を期すために:これをPopupScreenに追加するか(Ferminの回答のように)、Screenを直接オーバーライドして独自のダイアログを作成します。RIM API はスレッドセーフではないため、別のスレッドが必要です。イベント スレッドに関連するすべての UI を実行する必要があります (または、イベント ロックを保持している間は、 BlackBerry UI のスレッド化 - 非常に基本を参照してください)。
これはロード画面の簡単なコードです....
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();
アニメーションだけの場合は、ポップアップにアニメーション GIFを表示し、読み込み操作が完了したら閉じていただけますか?
この単純な実装を見てみることをお勧めします。私はこれが好きでしたが、使用したことはありません。あなたに役立つかもしれません。
最も簡単な方法は、おそらく標準の 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); を使用する update メソッドを用意します。プログレスバーを更新します。
私は通常、作業を行っているスレッドからこれを呼び出します (あなたの場合は読み込み、操作が完了するたびに進行状況バーが更新されます。