我正在写一个小的Android游戏,我想在每个关卡之前和游戏结束后显示一个启动屏幕。 我发现的大多数教程仅显示了在运行MainActivity之前运行的启动画面实现。
就我而言,我需要在MainActivity运行时“运行”启动画面。我该如何实现?
我已经创建了一个SplashActivity
类,可以根据教程扩展AppCompatActivity,但是我认为这不适合我的问题。
public class SplashActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}, 5000);
}
}
该活动具有以下xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.androidcodefinder.splashscreendemo.MainActivity"
android:background="@color/backgroundColor">
<ImageView
android:id="@+id/logo"
android:src="@mipmap/splash"
android:layout_width="120dp"
android:layout_height="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
在MainActivity
类中,我启动一个GamePanel extends SurfaceView
,然后启动GameLoop。
@Override
public void run () {
Canvas canvas;
while (!this.exit) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
playAnimation(R.drawable.splash_gameover, R.anim.viewanimation);
synchronized (this.surfaceHolder) {
this.gamePanel.update();
this.gamePanel.render(canvas, lag);
}
} finally {
if (canvas != null) {
this.surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
答案 0 :(得分:3)
您可以打电话
Intent i = new Intent(MainActivity.this, SplashActivity.class);
startActivity(i);
在MainActivity
中您想要打开SplashActivity
的任何地方。但是,您可能不希望SplashActivity
创建另一个新的MainActivtiy
。您可以通过Intent
传递一些消息,告诉SplashActivtiy
该怎么做。
在MainActivity
Intent i = new Intent(MainActivity.this, SplashActivity.class);
i.putExtra("START_NEW_MAIN",false)
startActivity(i);
在SplashActivity
public class SplashActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean startNewMain = getIntent().getBooleanExtra("START_NEW_MAIN",true)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
if(startNewMain)
{
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
}
finish();
}
}, 5000);
}
}
如果MainActivity
不是您应用的入口点,您甚至可以完全删除SplashActivity
中以SplashActivity
开头的部分。简单地:
在MainActivity
Intent i = new Intent(MainActivity.this, SplashActivity.class);
startActivity(i);
在SplashActivity
public class SplashActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 5000);
}
}
答案 1 :(得分:0)
在我的Android游戏 Anagram 中,它工作正常,我使用了以下代码,U可以将其检出-
我创建了一个像这样的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#fff"
tools:context=".SplashScreen">
<ImageView
android:id="@+id/imgLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="@drawable/splash" />
</LinearLayout>
@ drawable / splash是U想要显示的背景
下面是SplashScreen代码
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_TIME_OUT);
}
}