启动时/加载时的图像

11
我正在开发一个应用程序,在从onCreate点加载时,它只显示黑屏(直到应用程序运行起来)。看其他应用程序,它们会弹出一家公司的标志或酷炫的图像几秒钟,可以告诉我如何做吗?
另外,是否可以将其设置为最少时间显示?
3个回答

9
创建一个新的活动,显示图像几秒钟并重定向到您的主要活动:
public class SplashActivity extends Activity
{
    private static final long DELAY = 3000;
    private boolean scheduled = false;
    private Timer splashTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        splashTimer = new Timer();
        splashTimer.schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                SplashActivity.this.finish();
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }
         }, DELAY);
       scheduled = true;
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if (scheduled)
            splashTimer.cancel();
        splashTimer.purge();
    }
}

将您的图像设置为此活动的背景。希望这有所帮助。祝好运!


2
请使用Handler代替TimerTask - inazaruk
你在模拟器上还是真机上测试了这段代码?Android 上没有 Timer - Luke Vo
2
计时器不是Android对象,而是Java对象。这是我其中一个应用程序中的可工作代码。它可以在设备和模拟器上运行。将以下内容添加到您的导入列表中,您就可以使用它了:import java.util.Timer; import java.util.TimerTask; - Srichand Yella
1
延迟应用程序启动从来都不是一个好的策略。这个链接展示了一篇文章,解释了如何正确地实现它。核心思想是在应用程序进行初始处理时加载一张图片。 - Laranjeiro

1
这个启动图像也被称为“闪屏”。在这里你可以找到如何制作闪屏的方法。

0

您需要调用启动画面。这是我的启动画面代码。

只需添加新的活动并设置应用程序以打开此活动即可。

public class SplashActivity extends DeviceInfoAbstractActivity {

@SuppressLint("MissingSuperCall")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState, R.layout.activity_splash);

    passScreen();
}

private void passScreen() {

    new CountDownTimer(1000, 2000) {

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {

            Intent intent = RDAIntentHelpers.getClearCacheIntent();

            intent.setClass(SplashActivity.this, MainActivity.class);

            startActivity(intent);

        }
    }.start();
}

@Override
public void onBackPressed() {
    //no exit
}
}

这是我的getClearCacheIntent()方法

public static Intent getClearCacheIntent() {

    Intent intent = new Intent();

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    return intent;
}

在这之后,你的启动画面会停留在屏幕上2秒钟。你可以做任何想做的事情 =)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接