不使用意图淡出启动画面

时间:2014-01-10 03:14:56

标签: view webview splash-screen

所以我最近在我的应用程序中添加了一个启动画面,而不使用任何其他活动,因为我的大多数搜索都找到了。

一切都按预期工作,但我的目标是让它逐渐淡出我的视图,最好是在我的视图加载后才能加载webview。

在没有使用单独的活动和意图的情况下,完成初始屏幕淡出动画的最佳方法是什么?

以下是我主要活动中的工作代码。

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();
    }
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

由于您使用对话框作为启动画面,您可以在对话框中添加动画,可能请查看this

答案 1 :(得分: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);
          }
        }