Android 启动画面 ImageView 未加载
Android splash screen ImageView not loading
我的 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 教程真的帮了我大忙。我基于本教程的解决方案:
@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>
和我的styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/layers_splash</item>
</style>
</resources>
你可以这样做
- 通过以下Activity
(SplashScreen.java) 作为您的启动器 Activity(您应用的主要入口点)。
- 为初始图像创建单独的布局。
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;
}
}
运行 activity.init() 2 秒后显示启动画面如何?
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
activity.init();
}
}, 2000)
我的 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 教程真的帮了我大忙。我基于本教程的解决方案:
@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>
和我的styles.xml:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/layers_splash</item>
</style>
</resources>
你可以这样做
- 通过以下Activity (SplashScreen.java) 作为您的启动器 Activity(您应用的主要入口点)。
- 为初始图像创建单独的布局。
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;
}
}
运行 activity.init() 2 秒后显示启动画面如何?
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
activity.init();
}
}, 2000)