安卓 - 应用程序标志活动

4

我有一个看起来很简单的问题,但我无论如何都解决不了...

我有一个主Activity显示公司标志,实际上是一个启动画面。 我想要它显示2秒钟左右然后淡出到实际应用的主Activity。 我尝试使用sleep实现,但这样做会使我的标志Activity变成一个空白屏幕。 似乎在sleep结束之前图像并没有加载完毕。 所以,应用程序启动后会显示一个黑屏2秒钟,然后转到我的应用程序。 如果我点击返回按钮,我才能看到标志。 我在这里做错了什么? 这是我的标志代码。 logo.xml中有一个带有drawable资源的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);
    }
}
4个回答

8

你正在阻塞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);
    }
}

0

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

http://androidforums.com/40306-post4.html 另外,针对返回按钮问题,请添加

android:noHistory="true"

添加Logo Activity到清单文件中


0

使用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生命周期

但无论如何,你现在的做法是错误的 ;) 应该使用Handler、Thread或类似的方式来实现

其他解决方案:

  • 为什么不在所有内容前面显示一个ImageView,然后在2秒后隐藏它?你可以使用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中阻塞它一样不可取。 - Ted Hopp

0

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


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