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

}

R.style.dialog_anim_fade_out:

该style是/res/style.xml文件里面 resource节点下的节点,根据名字找到

<!-- 在item name里面指定android:windowExitAnimation,使用系统的名字表示什么时候执行后面的文件
    mce_bogus="1"大家都说删掉也没什么变化,我试了试果真如此
     -->

 <style name="dialog_anim_fade_out" parent="android:Animation" mce_bogus="1">
        <item name="android:windowExitAnimation">@anim/dialog_exit_fade_out</item>
    </style>

于是根据里面的item节点我们需要找res/anim/dialog_exit_fade_out.xml文件

dialog_exit_fade_out:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- android:interpolator="@android:anim/accelerate_interpolator"表示逐渐加速,就是速度不是均匀不变的,是退出的越来越快
    fromAlpha和toAlpha很好理解,duration也很好理解
     -->
    <alpha 
        android:interpolator="@android:anim/accelerate_interpolator"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="1000"/>
</set>
好了,终于解释完了这一小小部分,作者 www.longdw.com给该app增加了很好的扩展性,我们可以指定其他的退出方式,现在想想,当初为什么要给这个开源app做注释,就是因为里面的好多东西都值得借鉴,也许是看见过太多的垃圾教科书,除了千篇一律就是代码乱的一塌糊涂。在结束后我将,对整个app进行概括,然后再向作者致谢,(现在心里还没有底)我竟然搜到了他的主页就是上面的 www.longdw.com。。。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值