阅读背景:

SplashScreen代码分析_有速度的空气的博客

来源:互联网 
/**
 * 
 */

package com.example.music.utils;
import com.example.easymusic.R;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;


/**
 * 引导页图片,停留若干秒,然后自动消失。
 * 
 */
public class SplashScreen {
    
    public final static int SLIDE_LEFT = 1;
    public final static int SLIDE_UP = 2;
    public final static int FADE_OUT = 3;
    /**
     * 这个dialog是用来装root的,root是用来装imageview的,于是dialog就是imageview的核心
     */
    private Dialog splashDialog;
    
    private Activity activity;
    
    public SplashScreen(Activity activity){
        this.activity = activity;
    }
    
    /**
     * 显示。
     * @param imageResource 图片资源
     * @param millis 停留时间,以毫秒为单位。
     * @param animation 消失时的动画效果,取值可以是:SplashScreen.SLIDE_LEFT, SplashScreen.SLIDE_UP, SplashScreen.FADE
     */
    public void show(final int imageResource, final int animation){
        Runnable runnable = new Runnable() {
            public void run() {
                // Get reference to display
            	DisplayMetrics metrics = new DisplayMetrics();
//                Display display = activity.getWindowManager().getDefaultDisplay();

                // Create the layout for the dialog
                LinearLayout root = new LinearLayout(activity);
                //该linearlayout可以这样写下面/**/注释掉的
               /* root.setMinimumHeight(metrics.heightPixels);
                root.setMinimumWidth(metrics.widthPixels);*/
                root.setOrientation(LinearLayout.VERTICAL);
                root.setBackgroundColor(Color.BLACK);//可以没有,看不到效果
                //也可以这样写2个都写上多余,不报错
                root.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT, 0.0F));
                root.setBackgroundResource(imageResource);//看imagerResource的传递主线

                // Create and show the dialog
                splashDialog = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar);//没有title,Translucent表示半透明
                // check to see if the splash screen should be full screen
                //FLAG_FULLSCREEN = 1024 [0x400]也就是说在倒数第11位flags必须是1,当然也是1
                if ((activity.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN)
                        == WindowManager.LayoutParams.FLAG_FULLSCREEN) {
                    splashDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
                }
                
                Window window = splashDialog.getWindow();//通过splashDialog(本来是无title的dialog)来设置消失的动画
                switch(animation){
                //提供了3种退出动画,将在xml中就第三种解释他们的属性name都引用了android:这个命名空间的退出名字
                    case SLIDE_LEFT:
                        window.setWindowAnimations(R.style.dialog_anim_slide_left);
                        break;
                    case SLIDE_UP:
                        window.setWindowAnimations(R.style.dialog_anim_slide_up);
                        break;
                    case FADE_OUT:
                        window.setWindowAnimations(R.style.dialog_anim_fade_out);
                        break;
                }
            
                splashDialog.setContentView(root);//看imagerResource的传递主线imageResource->root->splashDialog
                splashDialog.setCancelable(false);
                splashDialog.show();

                // Set Runnable to remove splash screen just in case
                /*final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                        removeSplashScreen();
                    }
                }, millis);*/
            }
        };
        activity.runOnUiThread(runnable);
    }
    //调用该方法可以让该装饰类的装饰物dialog 给dismiss()
    public void removeSplashScreen(){
        if (splashDialog != null && splashDialog.isShowing()) {
            splashDialog.dismiss();
            splashDialog = null;
        }
    }

}
/**
 * 
 */

package com.example.music.utils;
i



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: