2

Android 2.1でスプラッシュスクリーンを書いています。スプラッシュ画面に20枚のpng画像を順番に表示させたい。animation-list を使おうとしましたが、うまくいきませんでした。

これが私のコードです。これで、firstLoadingImage.png という画像ができました。それだけが 5000ms 表示されます。次に、myappactivity が開始されます。しかし、この待ち時間の間に画像ソースを更新することができませんでした。どうすればこれができると思いますか?

スプラッシュスクリーン アクティビティ

package myapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

public class SplashScreenActivity extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 5000; // time to display the splash screen in ms

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);

        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(100);
                        if(_active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    startActivity(new Intent(SplashScreenActivity.this, MyAppActivity.class));
                    stop();
                }
            }
        };
        splashTread.start();
    }

}

スプラッシュスクリーン.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/linearLayout"
    android:background="@drawable/loading_screen_background" >

    <ImageView
        android:id="@+id/firstLoadingImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/loading100" />

</LinearLayout>
4

3 に答える 3

1

フレーム アニメーションはAnimationDrawable、 の背景に を設定することで実現できますImageView

例:

final AnimationDrawable anim = new AnimationDrawable();
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_frame1, durationInMs);
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_frame2, durationInMs);
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_frame3, durationInMs);
...
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_framen, durationInMs);
anim.setOneShot(false);

ImageView myImageView = getImageViewToSet();
myImageView.setBackgroundDrawable(anim);
myImageView.post(new Runnable(){
   public void run(){
      anim.start();
   }
}

したがって、スプラッシュ スクリーンを単純なレイアウトにするImageViewか、スプラッシュ スクリーンを作成してレイアウトに追加します。をそれに設定しAnimationDrawableて実行します。

于 2012-04-19T19:11:29.710 に答える
0

私の推測では、スプラッシュ スレッドで sleep を呼び出した後に関数 runOnUiThread() を使用することになります。ImageView の背景を変更する実行可能なクラスを作成し、おそらくこの関数で実行しますか? Activity.runOnUiThread()

于 2012-04-19T19:01:19.790 に答える
0

ここでチュートリアルを見つけることができます

于 2013-07-18T15:34:01.893 に答える