开始介绍Android应用的一个图片

时间:2013-03-13 16:39:38

标签: android android-layout android-imageview

我想为我的Android应用程序做一个介绍,所以我想这样做:

这是我的 intro.xml

   <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

    <ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/logo_inesc" />

  </LinearLayout>

想象我的 main.xml 带有一些菜单和图片。

当用户启动应用程序时,我想首先向他展示一个演示图像,然后向应用程序本身展示选项等等。

我在我的活动中这样做了:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.intro);

    try {
        Thread.sleep(6000); //Intro image will be shown for 6 seconds
        setContentView(R.layout.home);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我不知道这是否是正确的程序,布局正在改变,但图像没有显示。有人知道为什么吗?

问候。

3 个答案:

答案 0 :(得分:3)

虽然这个解决方案可能有效,但这样的事情可能更好:

public class SplashActivity extends Activity {
    protected boolean active = true;
    protected int splashTime = 1000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(active && (waited < splashTime)) {
                        sleep(100);
                        if(active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    // Start your Activity here
               }
           }
       };
       splashTread.start();    
   }
}

但是如果用户在启动延迟结束之前按下后退键(并关闭你的应用程序)会怎样。该应用程序可能仍会打开下一个活动,这不是真正用户友好的。

在GUI中进行睡眠也是不好的做法。

创建一个AsyncTask或另一个单独的线程。

This guy has a great solution where the splashscreen actually fades.

答案 1 :(得分:1)

  1. 如果只包含一个孩子,则不需要LinearLayout。
  2. 对Thread.sleep()使用AsyncTask,否则暂停UI线程

答案 2 :(得分:0)

您正在使当前线程(UI线程)休眠,为了暂停屏幕6秒,您需要创建一个单独的线程。

Thread t=new Thread(
new Runnable()
{
public void run()
{
sleep(6000);
}
}
);
t.start();

........

 setContentView(R.layout.intro);

    try {
        Thread t=new Thread(
    new Runnable()
    {
    public void run()
    {
    Thread.sleep(6000);
    }
    }
    );
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
finally
{
// start new activity with intent if you have a new activity or if you want to change the //contentView change here
setContentView(R.layout.home);
}
  t.start();