2014-01-10 43 views
0

因此,我最近添加了一個啓動畫面到我的應用程序,沒有使用任何其他活動,因爲我的大多數搜索找到了。不使用意圖淡出啓動畫面

一切正常,但我的目標是讓它淡出我的視圖被加載,最好在我的視圖加載後,因爲它的web視圖。

什麼是最好的方式來完成啓動畫面的淡出動畫,而不使用單獨的活動和意圖?

以下是我在主要活動中的工作代碼。

package com.chris.myapp; 

import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.ViewPager; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.Toast; 
import com.chris.myapp.adapter.ActionsAdapter; 
import com.chris.myapp.fragment.WebViewFragment; 
import shared.ui.actionscontentview.ActionsContentView; 
import android.app.*; 
import android.os.*; 

public class MainActivity extends FragmentActivity { 

    private static final String STATE_URI = "state:uri"; 

    private ActionsContentView viewActionsContentView; 
    private MyPagerAdapter pageAdapter; 
    private Dialog mSplashDialog; 

    private Uri currentUri = WebViewFragment.DEFAULT_URI; 
    @SuppressWarnings("deprecation") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     MyStateSaver data = (MyStateSaver) getLastNonConfigurationInstance(); 
     if (data != null) { 
      // Show splash screen if still loading 
      if (data.showSplashScreen) { 
       showSplashScreen(); 
      } 
      setContentView(R.layout.example);   

      // Rebuild your UI with your saved state here 
     } else { 
      showSplashScreen(); 
      setContentView(R.layout.example); 
      // Do your heavy loading here on a background thread 
     } 

     viewActionsContentView = (ActionsContentView) findViewById(R.id.actionsContentView); 
     viewActionsContentView.setSwipingType(ActionsContentView.SWIPING_EDGE); 
     viewActionsContentView.setSpacingType(ActionsContentView.SPACING_ACTIONS_WIDTH); 
     viewActionsContentView.setSpacingWidth(650); 
     viewActionsContentView.setActionsSpacingWidth(0); 

     pageAdapter = new MyPagerAdapter(getSupportFragmentManager()); 
     final ViewPager pager = (ViewPager) findViewById(R.id.myViewPager); 
     pager.setAdapter(pageAdapter); 
     pager.setPageMargin(20); 
     pager.setPageMarginDrawable(R.color.pager_bg); 

     final ListView viewActionsList = (ListView) findViewById(R.id.actions); 
     final ActionsAdapter actionsAdapter = new ActionsAdapter(this); 
     viewActionsList.setAdapter(actionsAdapter); 
     viewActionsList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapter, View v, int position, 
            long flags) { 
       final Uri uri = actionsAdapter.getItem(position); 
       updateContent(uri); 
       viewActionsContentView.showContent(); 
      } 
     }); 

     if (savedInstanceState != null) { 
      currentUri = Uri.parse(savedInstanceState.getString(STATE_URI)); 
     } 

     updateContent(currentUri); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     outState.putString(STATE_URI, currentUri.toString()); 

     super.onSaveInstanceState(outState); 
    } 

    public void updateContent(Uri uri) { 
     if (uri == null) 
      return; 

     final WebViewFragment webViewFragment = (WebViewFragment) 
     pageAdapter.getItem(MyPagerAdapter.WEBVIEW_FRAGMENT_POSITION); 
     webViewFragment.setUrl(uri.toString()); 
     webViewFragment.reload(); 
     currentUri = uri; 
    } 

    /** 
    * Removes the Dialog that displays the splash screen 
    */ 
    protected void removeSplashScreen() { 
     if (mSplashDialog != null) { 
      mSplashDialog.dismiss(); 
      mSplashDialog = null; 
     } 
    } 

    /** 
    * Shows the splash screen over the full Activity 
    */ 
    protected void showSplashScreen() { 
     mSplashDialog = new Dialog(this, R.style.SplashScreen); 
     mSplashDialog.setContentView(R.layout.splashscreen); 
     mSplashDialog.setCancelable(false); 
     mSplashDialog.show(); 

     // Set Runnable to remove splash screen just in case 
     final Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        removeSplashScreen(); 
       } 
      }, 3000); 
    } 

    /** 
    * Simple class for storing important data across config changes 
    */ 
    private class MyStateSaver { 
     public boolean showSplashScreen = false; 
     // Your other important fields here 
    } 


    private static long back_pressed; 
    private Toast toast; 
    @Override 
    public void onBackPressed() { 
     final WebViewFragment currentFragment = (WebViewFragment) 
       pageAdapter.getItem(MyPagerAdapter.WEBVIEW_FRAGMENT_POSITION); 
     if (currentFragment instanceof WebViewFragment) { 
      final WebViewFragment webFragment = currentFragment; 
      if (webFragment.onBackPressed()) 
       return; 
     } 

     if (back_pressed + 2000 > System.currentTimeMillis()) 
     { 

      // need to cancel the toast here 
      toast.cancel(); 

      // code for exit 
      Intent intent = new Intent(Intent.ACTION_MAIN); 
      intent.addCategory(Intent.CATEGORY_HOME); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(intent); 
      finish(); 

     } 
     else 
     { 
      // ask user to press back button one more time to close app 
      toast= Toast.makeText(getBaseContext(), "Press again to exit", Toast.LENGTH_SHORT); 
      toast.show(); 
      viewActionsContentView.showContent(); 
     } 
     back_pressed = System.currentTimeMillis(); 
    } 
} 

任何幫助將不勝感激。

回答

0

由於您使用對話框,閃屏,你可以動畫添加到對話框中,可能看this

+0

讓我走在正確的軌道上,發佈什麼對我有用。 – ChrisOSX

+0

很高興幫助。 :) –

0

結束了與下面的代碼搞清楚了這一點。希望它能幫助別人。

public void removeSplashScreen() { 
      if (mSplashDialog != null && mSplashDialog.isShowing()) { 
      final MainActivity that = this; 
      Runnable runnable = new Runnable() { 
       public void run() { 
       AnimationListener animationListener = new AnimationListener() { 

        public void onAnimationEnd(Animation animation) { 
        that.mSplashDialog.dismiss(); 
        that.mSplashDialog = null; 
        } 

       @Override 
       public void onAnimationRepeat(Animation animation) { 
        // TODO Auto-generated method stub 
       } 

       @Override 
       public void onAnimationStart(Animation animation) { 
        // TODO Auto-generated method stub 
       } 

       }; 
       AlphaAnimation splashDismissAnimation = new AlphaAnimation(1.0f, 0.0f); 
       splashDismissAnimation.setAnimationListener(animationListener); 
       splashDismissAnimation.setDuration(750); // Gives app some time to init. 

       View view = ((ViewGroup) ((Dialog) that.mSplashDialog).getWindow().getDecorView().getRootView()).getChildAt(0); 
       view.setBackgroundColor(Color.TRANSPARENT); 
       view.setBackgroundResource(android.R.color.transparent); 
       view.startAnimation(splashDismissAnimation); 
       } 
      }; 
      this.runOnUiThread(runnable); 
      } 
     }