启动安卓游戏前的黑屏

时间:2014-08-14 11:54:47

标签: java android

我在LibGDX中制作游戏,我注意到当我的游戏启动时,在我的游戏的白色屏幕被绘制之前会显示黑色屏幕一会儿。

这是我的启动画面类的代码:

public class SplashScreen extends Activity {

private final int welcomeScreenDisplay = 500;
private Intent intent = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setContentView(R.layout.main_view);

    Thread welcomeThread = new Thread() {

        int wait = 0;

        @Override
        public void run() {
            try {
                super.run();

                while (wait < welcomeScreenDisplay) {
                    sleep(100);
                    wait += 100;
                }
            } catch (Exception e) {
                System.out.println("EXc=" + e);
            } finally {

                finish();

                intent = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(intent);
            }
        }
    };
    welcomeThread.start();
}

}

主要活动:

public class MainActivity extends AndroidApplication implements RequestHandler {

private int idScreen = 0;
Context mContext;
Dialog dialog;
Button btn_no, btn_yes;
RelativeLayout rltMain;
View gameView;
private WebView ads;
ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    Bundle bundle = new Bundle();

    bundle = this.getIntent().getExtras();
    if (bundle != null) {
        idScreen = bundle.getInt("idScreen");
    }

    ads = new WebView(this);
    ads.getSettings().setJavaScriptEnabled(true);
    ads.loadUrl("file:///android_asset/ads.html");
    ads.setBackgroundColor(0x00000000);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    rltMain = new RelativeLayout(this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    gameView = initializeForView(new LettreGame(MainActivity.this,
            idScreen, this), false);
    rltMain.addView(gameView);

    rltMain.setLayoutParams(lp);
    ads.setLayoutParams(lp);

    img = new ImageView(this);
    img.setBackgroundDrawable(this.getResources().getDrawable(
            R.drawable.pub_img));
    System.out.println("Connectivity***************"
            + isConnected(MainActivity.this));
    if (isConnected(MainActivity.this) == true)
        rltMain.addView(ads);
    // else
    // rltMain.addView(img);
    setContentView(rltMain);
    ads.setVisibility(View.GONE);
    new ReloadWebView(this, 60, ads);
}

public void openPaintActivity() {
    Intent intent = new Intent(MainActivity.this, PaintActivity.class);
    mContext.startActivity(intent);
    this.finish();

}

protected void showCustomDialog() {

}

@Override
public void confirm(final ConfirmInterface confirmInterface) {

    gameView.post(new Runnable() {

        public void run() {

            dialog = new Dialog(MainActivity.this,
                    android.R.style.Theme_Translucent);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

            dialog.setCancelable(true);
            dialog.setContentView(R.layout.dialog);

            btn_yes = (Button) dialog.findViewById(R.id.btn_yes);
            btn_no = (Button) dialog.findViewById(R.id.btn_no);

            btn_yes.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    confirmInterface.yes();
                    dialog.dismiss();
                }
            });
            btn_no.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();

        }
    });
}

// ***********tester s'il y a une connexion internet
public static boolean isConnected(Context c) {

    ConnectivityManager connectivityManager = (ConnectivityManager) c
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        State networkState = networkInfo.getState();

        if (networkState.compareTo(State.CONNECTED) == 0) {
            return true;

        }
    }

    return false;
}

protected class ReloadWebView extends TimerTask {
    Activity context;
    Timer timer;
    WebView wv;

    public ReloadWebView(Activity context, int seconds, WebView wv) {
        this.context = context;
        this.wv = wv;
        timer = new Timer();
        timer.schedule(this, seconds * 1000, seconds * 1000);
    }

    @Override
    public void run() {
        if (context == null || context.isFinishing()) {
            this.cancel();
            return;
        }

        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                wv.reload();
            }
        });
    }
}

public void setContentViewVisible() {
    runOnUiThread(new Runnable() {
        public void run() {
            ads.setVisibility(View.VISIBLE);
        }
    });
}

public void playSound() {
    Settings.soundEnabled = !Settings.soundEnabled;
    if (Settings.soundEnabled)
        Assets.music.play();
    else
        Assets.music.pause();
}

}

0 个答案:

没有答案