android 应用显示空白屏幕而不是初始屏幕
android App showing Blank white screen instead of splash screen
我已经花了好几个小时了...有人能指出这里的问题吗?
这是我的 values/styles:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="android:windowDisablePreview">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.Transparent" parent="AppTheme">
<item name="android:windowDisablePreview">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
这是我的清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nuku.mc.myfypapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Transparent">
<activity
android:name=".SplashScreen"
android:theme="@style/Theme.Transparent"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan"
/>
<activity
android:name=".SignupActivity"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan" />
</application>
</manifest>
这是应用 splash_screen.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lin_lay"
android:background="@android:color/holo_orange_dark">
<ImageView
android:layout_width="360dp"
android:layout_height="480dp"
android:id="@+id/imageView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_orange_dark"
android:contentDescription="@string/splash_text"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/splash_text"
android:id="@+id/splashText"
android:layout_marginBottom="150dp"
android:layout_alignBottom="@+id/imageView2"
android:layout_centerHorizontal="true"
android:textStyle="bold|italic"
android:focusable="true"
android:textSize="@dimen/design_fab_image_size"
android:textColor="#ffffff" />
</RelativeLayout>
这是splash.java
public class SplashScreen extends Activity {
SessionManager manager;
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
manager = new SessionManager();
StartAnimations();
}
private void StartAnimations() {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
anim.reset();
RelativeLayout l=(RelativeLayout) findViewById(R.id.lin_lay);
l.clearAnimation();
l.startAnimation(anim);
anim = AnimationUtils.loadAnimation(this, R.anim.translate);
anim.reset();
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.clearAnimation();
iv.startAnimation(anim);
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 3 seconds
sleep(3*1000);
// After 5 seconds redirect to another intent
String status=manager.getPreferences(SplashScreen.this,"status");
Log.d("status",status);
if (status.equals("1")){
Intent i=new Intent(SplashScreen.this,MainActivity.class);
startActivity(i);
}else{
Intent i=new Intent(SplashScreen.this,LoginActivity.class);
startActivity(i);
}
//Remove activity
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
}
我已经学习了五个以上的教程,仍然无法解决问题
唯一看起来明显错误的是您在 onCreate 中调用 StartAnimations。您想改为在 onResume 中执行此操作。我还会尝试将其分解为多个步骤,以查看每个步骤是否都可以调试它。对我来说,第 1 步是将背景默认为红色,然后查看它是否显示。然后我会尝试将它设置为 50% 红色以查看 alpha 是否正常工作。然后我会尝试一次测试一个动画,首先是背景动画,然后是翻译动画。把它分解成更小的部分,你可以连续工作将使它更容易解决。
由于您是在 onCreate 中启动动画,所以我猜测您的动画在 Activity 首次呈现之前 运行 已经完成。请记住,直到在 onResume 中它才会呈现。由于白色是 activity 的默认颜色,您所看到的只是几秒钟的白色屏幕显示,然后是第二个 activity.
还考虑到您希望背景淡入的设计,您将需要在主题中设置 android:background,您在清单中设置为 activity。如果你不这样做,你将看到发生的事情(当你正确实施时)是一个持续约 0.5-2 秒的白屏,然后在它开始时变成透明 运行 你的闪屏动画.
关注this article。
它清楚地写着
Splash Screen is what the screen shows before it is able to start
inflating the UI is the window background that the theme of the launch
activity specifies. So the first thing we need is a special theme for
the launch activity.
我已经按照这篇文章实现了闪屏。所以您不需要创建任何启动画面 activity。只需将您的文本转换为可绘制资源,并将其用作启动画面主题的背景即可 activity。
我已经花了好几个小时了...有人能指出这里的问题吗?
这是我的 values/styles:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="android:windowDisablePreview">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.Transparent" parent="AppTheme">
<item name="android:windowDisablePreview">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
这是我的清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nuku.mc.myfypapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Transparent">
<activity
android:name=".SplashScreen"
android:theme="@style/Theme.Transparent"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan"
/>
<activity
android:name=".SignupActivity"
android:windowSoftInputMode="adjustPan" />
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan" />
</application>
</manifest>
这是应用 splash_screen.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lin_lay"
android:background="@android:color/holo_orange_dark">
<ImageView
android:layout_width="360dp"
android:layout_height="480dp"
android:id="@+id/imageView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_orange_dark"
android:contentDescription="@string/splash_text"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/splash_text"
android:id="@+id/splashText"
android:layout_marginBottom="150dp"
android:layout_alignBottom="@+id/imageView2"
android:layout_centerHorizontal="true"
android:textStyle="bold|italic"
android:focusable="true"
android:textSize="@dimen/design_fab_image_size"
android:textColor="#ffffff" />
</RelativeLayout>
这是splash.java
public class SplashScreen extends Activity {
SessionManager manager;
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
manager = new SessionManager();
StartAnimations();
}
private void StartAnimations() {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
anim.reset();
RelativeLayout l=(RelativeLayout) findViewById(R.id.lin_lay);
l.clearAnimation();
l.startAnimation(anim);
anim = AnimationUtils.loadAnimation(this, R.anim.translate);
anim.reset();
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.clearAnimation();
iv.startAnimation(anim);
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 3 seconds
sleep(3*1000);
// After 5 seconds redirect to another intent
String status=manager.getPreferences(SplashScreen.this,"status");
Log.d("status",status);
if (status.equals("1")){
Intent i=new Intent(SplashScreen.this,MainActivity.class);
startActivity(i);
}else{
Intent i=new Intent(SplashScreen.this,LoginActivity.class);
startActivity(i);
}
//Remove activity
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
}
我已经学习了五个以上的教程,仍然无法解决问题
唯一看起来明显错误的是您在 onCreate 中调用 StartAnimations。您想改为在 onResume 中执行此操作。我还会尝试将其分解为多个步骤,以查看每个步骤是否都可以调试它。对我来说,第 1 步是将背景默认为红色,然后查看它是否显示。然后我会尝试将它设置为 50% 红色以查看 alpha 是否正常工作。然后我会尝试一次测试一个动画,首先是背景动画,然后是翻译动画。把它分解成更小的部分,你可以连续工作将使它更容易解决。
由于您是在 onCreate 中启动动画,所以我猜测您的动画在 Activity 首次呈现之前 运行 已经完成。请记住,直到在 onResume 中它才会呈现。由于白色是 activity 的默认颜色,您所看到的只是几秒钟的白色屏幕显示,然后是第二个 activity.
还考虑到您希望背景淡入的设计,您将需要在主题中设置 android:background,您在清单中设置为 activity。如果你不这样做,你将看到发生的事情(当你正确实施时)是一个持续约 0.5-2 秒的白屏,然后在它开始时变成透明 运行 你的闪屏动画.
关注this article。 它清楚地写着
Splash Screen is what the screen shows before it is able to start inflating the UI is the window background that the theme of the launch activity specifies. So the first thing we need is a special theme for the launch activity.
我已经按照这篇文章实现了闪屏。所以您不需要创建任何启动画面 activity。只需将您的文本转换为可绘制资源,并将其用作启动画面主题的背景即可 activity。