我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
当前回答
创建一个活动,让我们活动命名为“a”,然后创建一个名为mysscreen .xml的xml文件,在其中设置一个启动画面图像作为背景,然后使用倒计时计时器从一个活动导航到另一个活动。要知道如何使用倒计时定时器看到我的答案在这个问题的TimerTask在Android?
其他回答
Create an Activity SplashScreen.java public class SplashScreen extends Activity { protected boolean _active = true; protected int _splashTime = 3000; // time to display the splash screen in ms @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splashscreen); Thread splashTread = new Thread() { @Override public void run() { try { int waited = 0; while (_active && (waited < _splashTime)) { sleep(100); if (_active) { waited += 100; } } } catch (Exception e) { } finally { startActivity(new Intent(SplashScreen.this, MainActivity.class)); finish(); } }; }; splashTread.start(); } } splashscreen.xml will be like this <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="600px" android:layout_height="1024px" android:background="#FF0000"> </RelativeLayout>
在默认情况下,启动屏幕不会自动使你的应用程序看起来更专业。一个专业设计的启动画面有可能使你的应用程序看起来更专业,但如果你不知道如何编写一个启动画面,那么你的应用程序的其他部分又会有多专业呢?
设置启动画面的唯一原因(借口)是你要做大量的计算,或者等待GPS/WiFi启动,因为你的应用程序在启动之前依赖于这些。如果没有这些计算的结果或访问GPS/WiFi(等等),你的应用程序是死在水里的,因此你觉得你需要一个启动画面,并且必须阻止任何其他正在运行的程序(包括后台)的屏幕视图。
这样的启动画面应该看起来像你的全屏应用程序,给人一种已经初始化的印象,然后在漫长的计算完成后,最终的细节可以填充(图像调整)。情况果真如此,或者说这是设计该计划的唯一方法的可能性非常小。
最好允许用户(和操作系统的其他部分)在等待的时候做一些其他的事情,而不是把你的程序设计成依赖于一些需要一段时间的事情(当等待的持续时间是不确定的)。
你的手机上已经有图标显示GPS/WiFi正在启动。启动画面所占用的时间或空间可以用于加载预计算或实际进行计算。请参阅下面的第一个链接,了解您创建的问题以及必须考虑的内容。
如果你必须等待这些计算或GPS/WiFi,最好是简单地让应用程序启动,并有一个弹出窗口,告诉你需要等待计算(一个TEXTUAL的“初始化”消息是可以的)。GPS/WiFi的等待是预期的(如果它们没有在另一个程序中启用),所以宣布他们的等待时间是不必要的。
请记住,当启动屏幕启动时,你的程序实际上已经在运行,你所做的只是延迟程序的使用,并占用CPU/GPU来做一些大多数人认为没有必要的事情。
我们最好真的想要等待和看到你的启动屏幕,每次我们开始你的程序,否则我们不会觉得这是非常专业的编写。将启动画面设置为全屏,并复制实际程序的屏幕(所以我们认为它是初始化的,但实际上它并没有初始化)可能会实现你的目标(让你的程序看起来更专业),但我不敢打赌这一点。
为什么不去做呢:http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/
怎么做:https://encrypted.google.com/search?q=Android+splash+screen+source
所以有一个很好的理由不这样做,但如果你确定你的情况在某种程度上超出了这些例子,那么这样做的方法是上面给出的。一定要让你的申请看起来更专业,否则你就失去了这么做的唯一理由。
它就像一个YouTube频道,每个视频都以冗长的图形介绍(和outo)开始,或者觉得有必要讲一个笑话或解释过去一周发生的事情(当它不是喜剧或生活方式频道时)。秀一秀吧!(只需运行程序)。
这里有一个简单的!
~卢诺克斯
MainActivity.java
package com.example.splashscreen;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
splashscreen.java
package com.example.splashscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class splashscreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
//Splash Screen duration
int secondsDelayed = 1;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(splashscreen.this, MainActivity.class));
finish();
}
}, secondsDelayed * 3000);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
splashscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splashlogo"
/>
splashlogo.png
splashlogo.png
GitHub
SplashScreen
非常简单的方法:
首先使用以下网站创建你的splash: https://www.norio.be/android-feature-graphic-generator/ 选择你的标志和口号,选择你美丽的背景。将其大小调整为4096x4096。 现在下载图片并上传至: https://apetools.webprofusion.com/app/#/tools/imagegorilla 生成所有需要的启动画面,所有设备,所有平台。
享受吧!
这里有足够的答案,将有助于实施。这篇文章旨在帮助其他人完成创建启动画面的第一步!
有时用户打开SplashActivity并立即退出,但应用程序仍然在SPLASH_SCREEN_DISPLAY_LENGTH后转到MainActivity。
为了防止它:在SplashActivity中,你应该在移动到MainActivity之前检查SplashActivity是否结束
public class SplashActivity extends Activity {
private final int SPLASH_SCREEN_DISPLAY_LENGTH = 2000;
@Override
public void onCreate(Bundle icicle) {
...
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (!isFinishing()) {//isFinishing(): If the activity is finishing, returns true; else returns false.
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}, SPLASH_SCREEN_DISPLAY_LENGTH);
}
}
}
希望这对你有所帮助