我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
当前回答
阿卜杜拉的回答很好。但我想在我的回答中补充一些细节。
实现启动画面
正确执行闪屏的方法与你想象的略有不同。您所看到的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/ 感谢这些人把我推向正确的方向。我想要帮助其他人,因为公认的答案并不是做启动画面的建议。
其他回答
一个超级灵活的启动屏幕如何,可以使用相同的代码,并在AndroidManifest.xml中定义,因此代码永远不需要更改。我通常开发代码库,不喜欢定制代码,因为它很草率。
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="launch_class" android:value="com.mypackage.MyFirstActivity" />
<meta-data android:name="duration" android:value="5000" />
</activity>
然后SpashActivity本身查找“launch_class”的元数据,然后创建Intent本身。元数据“持续时间”定义了启动画面持续的时间。
public class SplashActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_splash);
ComponentName componentName = new ComponentName(this, this.getClass());
try {
Bundle bundle = null;
bundle = getPackageManager().getActivityInfo(componentName, PackageManager.GET_META_DATA).metaData;
String launch_class = bundle.getString("launch_class");
//default of 2 seconds, otherwise defined in manifest
int duration = bundle.getInt("duration", 2000);
if(launch_class != null) {
try {
final Class<?> c = Class.forName(launch_class);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, c);
startActivity(intent);
finish();
}
}, duration);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
启动画面示例:
public class MainActivity extends Activity {
private ImageView splashImageView;
boolean splashloading = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
splashImageView = new ImageView(this);
splashImageView.setScaleType(ScaleType.FIT_XY);
splashImageView.setImageResource(R.drawable.ic_launcher);
setContentView(splashImageView);
splashloading = true;
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
splashloading = false;
setContentView(R.layout.activity_main);
}
}, 3000);
}
}
我在android中使用线程制作Flash屏幕。
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public class HomeScreen extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_home);
Thread thread = new Thread(){
public void run(){
try {
Thread.sleep(3 * 1000);
Intent i = new Intent(HomeScreen.this, MainActivity.class);
startActivity(i);
} catch (InterruptedException e) {
}
}
};
thread.start();
}
}
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>
它真的很简单,在安卓,我们只是使用处理器的概念来实现启动画面
在SplashScreenActivity java文件中粘贴此代码。
在SplashScreenActivity xml文件中使用imageview放置任何图片。
public void LoadScreen() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreenActivity.this, AgilanbuGameOptionsActivity.class);
startActivity(i);
}
}, 2000);
}