我试图创建一个应用程序,显示第一个布局5秒,然后进入索引布局(Home)。 第一个布局仅显示徽标和应用程序名称...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SystemClock.sleep(5000);
Intent i = new Intent(this, Home.class);
startActivity(i);
}
如果我对SystemClock.sleep(5000); Intent i = new Intent(this, Home.class); startActivity(i);
发表评论,应用就像我预期的那样。显示第一个布局,保持这种布局(显然,我没有发布任何其他内容:P)。但是,如果我对它们进行了解除,则活动会等待5秒而不显示任何内容(空白布局)。
有什么想法吗?
谢谢你们。
答案 0 :(得分:2)
请勿在您的应用中强制等待显示启动画面,否则会浪费用户时间。要以正确的方式执行启动画面,您可以使用应用主题中的windowBackground
选项。以下是一个示例:https://www.bignerdranch.com/blog/splash-screens-the-right-way/
答案 1 :(得分:1)
我猜你正在寻找像Splash Screen这样的东西,它显示几秒钟并且主要活动开始了?
你可以使用这样的东西:
public class ActivitySplash extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_splash);
Firebase.setAndroidContext(this);
starter();
}
//Thread for the timer with 3.5 secs.
private void starter()
{
Thread timer = new Thread(){
public void run(){
try{
//Sleep for 5 seconds.
sleep(5000);
}
catch(InterruptedException e){
e.printStackTrace();
} finally {
//Call and start the new Activity
Intent newActivity= new Intent(ActivitySplash.this, YOURACTIVITY.class);
startActivity(newActivity);
finish();
}
}
};
timer.start();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
}
要在布局中显示徽标,只需在中心创建一个带有要输入名称的徽标。这是我使用的那个:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ActivitySplash">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@mipmap/ic_launcher"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
答案 2 :(得分:1)
试试这个..
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(MainActivity.this, Home.class);
startActivity(i);
}
}, 5000);
而不是
SystemClock.sleep(5000);
答案 3 :(得分:1)
为了在您的应用上显示启动画面,您不应该显示布局,尝试让主线程休眠5秒钟并再次唤醒应用程序。 &#34;权利&#34;方式(没有正确的方式显示启动画面,因为在移动应用程序上几乎禁止启动画面)显示启动画面将创建一个新的活动或片段,其中包含此屏幕,并使用Handler.postDelayed在此活动中,等待5秒钟,然后显示您的主要活动。
有关启动画面的有用信息:
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
答案 4 :(得分:1)
不要创建处理程序和线程,这是不必要的! 您的任务可以通过这种方式完成:
getWindow().getDecorView().postDelayed(new Runnable() {
@Override
public void run() {
//start activity here
}
}, 500);
不要为垃圾收集器创建更多垃圾。
虽然,你应该提到ActivityTransition。那真的是你在找?如果没有,您可以简单地添加到布局启动视图,这将覆盖所有内容,您可以在此启动视图上播放自定义动画。例如:
getWindow().getDecorView().postDelayed(new Runnable() {
@Override
public void run() {
// splash would disappear after 400 ms
mSplashView.animate().alpha(0).setDuration(400).start();
}
}, 500);