我的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>
答案 0 :(得分:1)
你可以做这样的事
<强> 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)