我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
我想让我的应用看起来更专业,所以我决定添加一个启动画面。
我应该如何进行实现呢?
当前回答
在Kotlin中编写以下代码:-
Handler().postDelayed({
val mainIntent = Intent(this@SplashActivity, LoginActivity::class.java)
startActivity(mainIntent)
finish()
}, 500)
希望这对你有所帮助,谢谢........
其他回答
一个超级灵活的启动屏幕如何,可以使用相同的代码,并在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();
}
}
}
虽然有很好的答案,但我将展示谷歌推荐的方法:
1)首先为启动画面创建一个主题: 你有一个名为splashscreenTheme的主题,你的启动器主题将是:
<style name="splashscreenTheme">
<item name="android:windowBackground">@drawable/launch_screen</item>
</style>
注意:
android:windowBackground已经设置了你的溅屏图像编号 需要在UI中再次这样做。
你也可以在这里使用颜色来代替绘图。
2)设置主题为splashscreenActivity的manifest
<activity
android:name=".activity.splashscreenActivity"
android:screenOrientation="portrait"
android:theme="@style/splashscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
3)确保你的launch_screen drawable不在drawable文件夹如果你的图像不小。
这将导致更快的启动屏幕启动和拯救你从黑屏
这也避免了额外的透支
这里有一个简单的!
~卢诺克斯
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
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 " /> < /意图过滤器> > < /活动
启动画面示例:
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);
}
}