Social Icons

2013年2月16日 星期六

啟動螢幕Splash Screen的設計

啟動螢幕Splash Screen常常用在程式一開始的時候,特別是用來顯示公司的logo。 這邊要介紹實作splash screen的兩種方式,第一種是利用兩個不同的xml layout檔案,第二種是寫在同一個xml layout檔案中。




 一,寫在不同xml layout的方式,就是建立一個Handler,一定時間後呼叫setContentView去設定新的layout檔案就可以了。不過記得程式中用到的widget(例如findViewById)要在設定完新的layout檔案結束後再去設定哦。
public class Splash extends Activity {
    private final int SPLASH_DISPLAY_LENGTH = 2000; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle icicle) { 
         super.onCreate(icicle); 
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.splash); 
         
         /* New Handler to start the Menu-Activity 
          * and close this Splash-Screen after some seconds.*/ 
         new Handler().postDelayed(new Runnable(){ 
              @Override 
              public void run() { 
                   /* Create an Intent that will start the Menu-Activity. */ 
                   //Intent mainIntent = new Intent(Splash.this,Hello.class); 
                   //Splash.this.startActivity(mainIntent); 
                   //Splash.this.finish(); 
               jumpToMainLayout();
              } 
         }, SPLASH_DISPLAY_LENGTH); 
    } 
    
    public void jumpToMainLayout()
    {
     setContentView(R.layout.main);
    }
}
二,寫在同一個xml中,這種方法其實是將兩個Activity的layout整合整合在一個layout檔和一個Activity中,在layout中利用view的visibility屬性來切換要顯示的頁面。其實重點都在設定一個Handler,當時間到了的時候自動轉換到下一個顯示頁面。這個的方法的好處是在onStart,onResume的時間點不需要考慮主要的畫面是否已經跳到主畫面,而且如果要顯示特別個過場動畫,也比較容易完成哦。
public class ShowSong extends Activity {
    private final int SPLASH_DISPLAY_LENGTH = 500; 

 private Preference mPref;
 private SongDB mSDB;
 private ImageView mLogo;
 private LinearLayout mSongView;
 private LinearLayout mSongsView;
 private EditText mSearchBox;
 private ListView mSongsList;
 private SongItemAdapter mSongsAda;
 private Song mSong;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mPref = new Preference(this);
        mPref.initSongDB();
        mSDB = new SongDB(mPref.getSongFolder());
        mLogo = (ImageView)findViewById(R.id.logo);
        mSongView = (LinearLayout)findViewById(R.id.song_view);
        mSongsView = (LinearLayout)findViewById(R.id.songs_view);
        mSearchBox = (EditText)findViewById(R.id.search_box);
        mSongsList = (ListView)findViewById(R.id.songs_list);
        mSearchBox.addTextChangedListener(new TextWatcher() {
   @Override
   public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub
   }
   @Override
   public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // TODO Auto-generated method stub
   }
   @Override
   public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (mSongsAda != null) {
     mSongsAda.getFilter().filter(s);
    }
   }
        });
        //mSongsList.setTextFilterEnabled(true);
        mSongsList.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
    Song song = (Song)mSongsAda.getItem(arg2);
    if (song != null) {
     doSongView(song);
    }
   }
        });

        /* New Handler to start the Menu-Activity 
         * and close this Splash-Screen after some seconds.*/ 
        new Handler().postDelayed(new Runnable(){ 
             @Override 
             public void run() { 
                  /* Create an Intent that will start the Menu-Activity. */ 
              toMainLayout();
             } 
        }, SPLASH_DISPLAY_LENGTH); 
    }
    
    public void toMainLayout()
    {
     mSongsView.setVisibility(View.VISIBLE);
     mSongsView.setClickable(true);
  mSongsView.setAnimation(AnimationUtils.loadAnimation(ShowSong.this, R.layout.push_right_in));
 mLogo.setVisibility(View.GONE);
 listSongs();
    }

沒有留言:

張貼留言