启动/加载时的图像

Pau*_*aul 9 android image loading oncreate

我正在开发一个应用程序,当它从onCreate点加载时,我只是有一个黑屏(直到应用程序站稳脚跟).看看其他应用程序,他们有一个公司徽标或酷炫的图像弹出几秒钟,有人可以告诉我该怎么做呢?

如果您可以将其设置为显示最短时间?

Sri*_*lla 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 但请使用`Handler`而不是`TimerTask`. (2认同)
  • Timer不是一个android对象,它是一个java对象.这是我的一个应用程序中的工作代码.它适用于设备和模拟器.将以下内容添加到导入列表中,您就可以了:import java.util.Timer; import java.util.TimerTask; (2认同)