我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
当前回答
在Android中,启动画面是一个有点不可用的对象:为了隐藏主活动启动的延迟,它不能尽快加载。使用它有两个原因:广告和网络运营。
实现为对话框使跳跃没有延迟从启动画面到主UI的活动。
public class SplashDialog extends Dialog {
ImageView splashscreen;
SplashLoader loader;
int splashTime = 4000;
public SplashDialog(Context context, int theme) {
super(context, theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
setCancelable(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
cancel();
}
}, splashTime);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white">
<ImageView
android:id="@+id/splashscreen"
android:layout_width="190dp"
android:layout_height="190dp"
android:background="@drawable/whistle"
android:layout_centerInParent="true" />
</RelativeLayout>
并开始:
public class MyActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().getCategories() != null && getIntent().getCategories().contains("android.intent.category.LAUNCHER")) {
showSplashScreen();
}
}
protected Dialog splashDialog;
protected void showSplashScreen() {
splashDialog = new SplashDialog(this, R.style.SplashScreen);
splashDialog.show();
}
...
}
其他回答
在Kotlin中编写以下代码:-
Handler().postDelayed({
val mainIntent = Intent(this@SplashActivity, LoginActivity::class.java)
startActivity(mainIntent)
finish()
}, 500)
希望这对你有所帮助,谢谢........
阿卜杜拉的回答很好。但我想在我的回答中补充一些细节。
实现启动画面
正确执行闪屏的方法与你想象的略有不同。您所看到的splash视图必须立即准备好,甚至在您可以在splash活动中膨胀布局文件之前。
所以你不会使用布局文件。相反,指定启动画面的背景作为活动的主题背景。为此,首先在res/drawable中创建一个XML drawable。
background_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Background Color -->
<item
android:drawable="@color/gray"/>
<!-- Logo -->
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
它只是一个层列表与标志在中心背景色与它。
现在打开styles.xml并添加这个样式
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
这个主题将没有动作栏和我们刚才创建的背景。
在manifest中,你需要将SplashTheme设置为你想要用作splash的活动。
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
然后在你的活动代码中,使用意图引导用户进入特定的屏幕。
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
这才是正确的做法。我用这些参考来回答。
https://material.google.com/patterns/launch-screens.html https://www.bignerdranch.com/blog/splash-screens-the-right-way/ 感谢这些人把我推向正确的方向。我想要帮助其他人,因为公认的答案并不是做启动画面的建议。
注意这个解决方案不会让用户等待更长的时间:启动画面的延迟取决于应用程序的启动时间。
当你打开任何android应用程序时,默认情况下你会看到一个黑屏,上面是应用程序的标题和图标,你可以通过使用样式/主题来改变这种情况。
首先,在values文件夹中创建一个style.xml,并向其添加一个样式。
<style name="splashScreenTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
</style>
而不是使用@android:style/Theme.DeviceDefault.Light。NoActionBar你可以使用任何其他主题作为父主题。
其次,在你的应用Manifest.xml中添加android:theme="@style/splashScreenTheme"到你的主活动。
<activity
android:name="MainActivity"
android:label="@string/app_name"
android:theme="@style/splashScreenTheme" >
第三,在onCreate()启动活动中更新主题。
protected void onCreate(Bundle savedInstanceState) {
// Make sure this is before calling super.onCreate
setTheme(R.style.mainAppTheme);
super.onCreate(savedInstanceState);
}
更新 看看这篇文章。
感谢@mat1h和@adelriosantiago
在我的例子中,我不想创建一个新的活动只是为了显示2秒的图像。当启动我的MainActivity,图像被加载到持有者使用毕加索,我知道这需要大约1秒的加载,所以我决定做以下在我的MainActivity OnCreate:
splashImage = (ImageView) findViewById(R.id.spllll);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
splashImage.setVisibility(View.GONE);
}
}, secondsDelayed * 2000);
当启动应用程序时,发生的第一件事是显示ImageView,并通过将窗口标志设置为全屏来删除状态栏。然后我使用Handler运行2秒,2秒后我清除全屏标志,并设置ImageView的可见性为GONE。简单,简单,有效。
Create an activity: Splash Create a layout XML file: splash.xml Put UI components in the splash.xml layout so it looks how you want your Splash.java may look like this: public class Splash extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); int secondsDelayed = 1; new Handler().postDelayed(new Runnable() { public void run() { startActivity(new Intent(Splash.this, ActivityB.class)); finish(); } }, secondsDelayed * 1000); } } change ActivityB.class to whichever activity you want to start after the splash screen check your manifest file and it should look like
<活动 android: name = "。屏” android: label = " @string / app_name " > > < /活动 <活动 android: name = "。飞溅” android: label = " @string / title_activity_splash_screen " > <意图过滤器> <action android:name="android.intent.action. main " /> <category android:name="android.intent.category. launcher " /> < /意图过滤器> > < /活动