简体   繁体   English

Android启动画面ImageView无法加载

[英]Android splash screen ImageView not loading

My Android application is supposed to show a splash screen consisting solely of an ImageView while it is still loading. 我的Android应用程序应该在仍加载时显示一个仅由ImageView组成的启动屏幕。 The ImageView should just display a local resource. ImageView应该只显示本地资源。 Here is my code: 这是我的代码:

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

My problem is that as soon as I execute code after the first call for setContentView(), my splash screen image is not displayed and the new layout immediately shows up. 我的问题是,在第一次调用setContentView()之后执行代码后,我的启动屏幕图像不会显示 ,并且新布局立即显示出来。 I would appreciate any help on how to solve this problem. 我将不胜感激如何解决此问题。

EDIT: It was my fault to assume that my code was the ideal solution for a splash screen. 编辑:认为我的代码是启动屏幕的理想解决方案是我的错。 This tutorial really helped me out. 教程确实帮助了我。 My solution based on this tutorial: 我的解决方案基于本教程:

@drawable/layer-list_splash : @ drawable / layer-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>

and my styles.xml : 和我的styles.xml

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

You can Do Something Like This 你可以做这样的事情

  1. By Making This Following Activity (SplashScreen.java) as Your Launcher Activity(Main Entry point for your app). 通过将此后续活动(SplashScreen.java)用作启动器活动(应用程序的主入口点)。
  2. Create A Separate Layout for splash image. 为启动图像创建单独的布局。

splash.xml 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 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;
    }
}

How about running the activity.init() after 2 secs to show the splash screen? 2秒后运行activity.init()以显示初始屏幕如何?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM