Android启动画面ImageView无法加载

时间:2015-12-12 18:33:37

标签: android imageview splash-screen

我的Android应用程序应该在加载时显示一个仅由ImageView组成的启动画面。 ImageView应该只显示本地资源。这是我的代码:

public class GameActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ImageView splash = new ImageView(this);
        splash.setImageResource(R.drawable.splashImage);

        RelativeLayout layout = new RelativeLayout(this);
        layout.setId(R.id.gameView);
        uilayout addView(splash, someLayoutParams);
        setContentView(layout, someOtherLayoutParams);
        // with a return-statement, the image would be displayed now

        final GameActivity activity = this;
        this.findViewById(R.id.gameView).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // this is supposed to wait until the image has been fully loaded
                activity.init(this);
            }
        });
    }

    public void init(ViewTreeObserver.OnGlobalLayoutListener listener) {
        try {
            findViewById(R.id.gameView).getViewTreeObserver().removeOnGlobalLayoutListener(listener);
        } catch (NoSuchMethodError x) {
            findViewById(R.id.gameView).getViewTreeObserver().removeGlobalOnLayoutListener(listener);
        } finally {
            LinearLayout layout = new LinearLayout(this);
            // lots of stuff
            setContentView(layout);
        }
    }
}

我的问题是,只要在第一次调用setContentView()后执行代码,我的启动画面图像就不会显示,新的布局会立即显示出来。 我很感激有关如何解决这个问题的任何帮助。

修改 假设我的代码是启动画面的理想解决方案,这是我的错。 This教程真的帮助了我。我的解决方案基于本教程:

@绘制/层list_splash

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/color_background"/>

    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/sprite_splash"/>
    </item>
</layer-list>

和我的 styles.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/layers_splash</item>
    </style>
</resources>

2 个答案:

答案 0 :(得分:1)

你可以做这样的事

  1. 进行以下活动 (SplashScreen.java)作为您的启动器活动(应用程序的主要入口点)。
  2. 为启动图像创建单独的布局。
  3. <强> splash.xml

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/TheSplashLayout"
        android:layout_gravity="center"
        >
        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/SplashImageView"
            android:layout_gravity="center"
            android:src="@drawable/your_splash_image"
            />
    </LinearLayout>
    

    SplashScreen.java

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.MotionEvent;
    
    import com.example.harsh.gmfbp.R;
    
    public class SplashScreen extends Activity {
    
        /**
         * The thread to process splash screen events
         */
        private Thread mSplashThread;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Splash screen view
            setContentView(R.layout.splash);
    
            final SplashScreen sPlashScreen = this;
    
            // The thread to wait for splash screen events
            mSplashThread =  new Thread(){
                @Override
                public void run(){
                    try {
                        synchronized(this){
                            // Wait given period of time or exit on touch
                            wait(5000);
                        }
                    }
                    catch(InterruptedException ex){
                    }
    
                    finish();
    
                    // Run next activity which is your GameActivity
                    Intent intent = new Intent();
                    intent.setClass(sPlashScreen, MainActivity.class); //Here You Can Replace MainActivity.class with your GameActivity
    
                    startActivity(intent);
    
                }
            };
    
            mSplashThread.start();
        }
    
        /**
         * Processes splash screen touch events
         */
        @Override
        public boolean onTouchEvent(MotionEvent evt)
        {
            if(evt.getAction() == MotionEvent.ACTION_DOWN)
            {
                synchronized(mSplashThread){
                    mSplashThread.notifyAll();
                }
            }
            return true;
        }
    }
    

答案 1 :(得分:1)

如何在2秒后运行activity.init()以显示启动画面?

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            activity.init();
        }
    }, 2000)