【问题标题】:Android - Application Logo ActivityAndroid - 应用程序徽标活动
【发布时间】:2013-07-07 21:30:49
【问题描述】:

我有一个看似简单的问题,但我这辈子都想不通......

我有一个显示公司徽标的主 Activity,本质上是一个启动屏幕。我希望它显示 2 秒左右并淡出到实际的应用程序主要活动。我尝试使用睡眠来实现,但是这样做会给我一个用于徽标活动的空白屏幕。似乎直到睡眠完成后才加载图像。所以基本上应用程序启动,显示黑屏 2 秒,然后转换到我的应用程序。如果我点击返回,我会看到徽标。我在这里做错了什么?这是我的标志代码。 logo.xml 有一个带有可绘制资源的 ImageView:

public class Logo extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logo);

        // Intent to jump to the next activity
        Intent intent= new Intent(this, NextActivity.class);
        this.startActivity(intent);

        SystemClock.sleep(2000);
    }
}

【问题讨论】:

  • 感谢泰德!这很有帮助

标签: android android-intent android-activity


【解决方案1】:

您正在阻塞 UI 线程,这是一个很大的禁忌。在您的onCreate 方法返回之前,系统无法绘制屏幕。做你想做的事的一种常见方法是启动一个单独的线程等待,然后将 Runnable 发布到 UI 线程:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.logo);
    final Handler handler = new Handler();
    final Runnable doNextActivity = new Runnable() {
        @Override
        public void run() {
            // Intent to jump to the next activity
            Intent intent= new Intent(this, NextActivity.class);
            startActivity(intent);
            finish(); // so the splash activity goes away
        }
    };

    new Thread() {
        @Override
        public void run() {
            SystemClock.sleep(2000);
            handler.post(doNextActivity);
        }
    }.start();
}

一种更简单的方法(正如 Athmos 在他的回答中建议的那样)是让处理程序为您进行倒计时:

Handler mHandler;
Runnable mNextActivityCallback;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.logo);
    mHandler = new Handler();
    mNextActivityCallback = new Runnable() {
        @Override
        public void run() {
            // Intent to jump to the next activity
            Intent intent= new Intent(this, NextActivity.class);
            startActivity(intent);
            finish(); // so the splash activity goes away
        }
    };
    mHandler.postDelayed(mNextActivityCallback, 2000L);
}

这样做的好处是您可以取消继续进行下一个活动(例如,如果用户按下“后退”按钮,或者您在这 2 秒内检测到错误情况或其他情况):

@Override
protected void onPause() {
    if (isFinishing()) {
        mHandler.removeCallbacks(mNextActivityCallback);
    }
}

【讨论】:

    【解决方案2】:

    不要在构造函数中使用睡眠,这会将视图的构建延迟 2 秒,这就是为什么它仅在 2 秒后显示图片,请尝试遵循此处的建议

    http://androidforums.com/40306-post4.html 另外对于后退按钮问题添加

    android:noHistory="true"
    

    到 Logo Activity 的清单

    【讨论】:

      【解决方案3】:

      使用 onResume() 和 Handler,简单干净的方式

      你试过把这个...

      // Intent to jump to the next activity
      Intent intent= new Intent(this, NextActivity.class);
      SystemClock.sleep(2000); //NOTE before the start of the next activity
      this.startActivity(intent);
      

      ...在 onResume();

      据我所知,在调用 onCreate() 时不会正确显示 Activity。但是 onResume() 总是被调用,据我所知,到那时应该显示一切。 如果您只想展示一次,请为它制作旗帜。 见:Activity lifecycle

      但无论如何,你做错了;)为此使用处理程序、线程或类似的

      其他解决方案:

      • 为什么不在所有内容的前面显示一个 ImageView 而只是隐藏它 2s后?您可以使用Handler 来延迟。甚至使用 透明度以制作精美的淡出效果。看ImageView setAlpha(...)
      • 为什么不使用Handler 而不是sleep(...)。恕我直言sleep(...) 可能会导致“应用程序无响应”

      这段代码应该可以工作:

      Handler mHandler = new Handler();
      
      class MyTask implements Runnable{
          public void run(){
              //start next activity or fadeout ImageView or ...
          }
      }
      

      现在调用 onResume:

      mHandler.postDelayed(new MyTask(), 100); //or any other Runnable
      

      【讨论】:

      • onResume 中阻止 UI 线程与在 onCreate 中阻止它一样大。
      【解决方案4】:

      你应该在新线程中使用 sleep ,因为它会停止执行 oncreate 方法,你可以在设置 setcontentview 后使用它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多