2013-03-13 55 views
0

我想爲我的Android應用程序的介紹一個圖像開始前奏,所以我想這樣做,以這種方式:與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(); 
    } 
} 

我不知道這是做正確的程序,佈局正在發生變化正確,但圖像沒有被顯示。有人知道爲什麼嗎?

問候。

+1

您是否在嘗試介紹該應用?如果是這樣,試試這個庫,https://github.com/PaoloRotolo/AppIntro – 2015-09-10 08:02:11

回答

0

您正在使當前線程(UI線程)sleep,爲了暫停屏幕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(); 
+0

Downvoter應該有一個敢於評論 – Pragnani 2013-03-13 16:45:07

+2

我會想象投票是從你的答案沒有什麼比無法編譯更多。即使它編譯了,它仍然只會產生一個6秒內不執行任何操作的線程。 – 2013-03-13 17:22:53

+0

@JustinBreitfeller不要在想象中存在,在實踐中,我並不反對downvote,相反,我會歡迎它,但問題是答案中的錯誤。所以我需要編寫他的整個代碼而不是給出想法。?對,爲什麼你認爲這個代碼會失敗..?你發現了什麼錯誤讓我知道。 – Pragnani 2013-03-13 17:36:50

1
  1. ,如果它包含只有一個孩子,你並不需要一個LinearLayout中。
  2. 使用的的AsyncTask的Thread.sleep()方法,否則你暫停UI線程
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.