Android Activity加入半透明蒙板,实现夜间模式

public void night() {
        if (mNightView == null) {
            mNightView = new TextView(this);
            mNightView.setBackgroundColor(Color.BLACK);
            windowLayout.addView(mNightView);
        }
        try {
            Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                    this, com.aurora.R.anim.aurora_menu_cover_enter);
            mNightView.startAnimation(hyperspaceJumpAnimation);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void day() {
        try {
            Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                    this, com.aurora.R.anim.aurora_menu_cover_exit);
            mNightView.startAnimation(hyperspaceJumpAnimation);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }


private View mNightView = null;

private WindowManager mWindowManager;

@Override

protected void onCreate(Bundle savedInstanceState) {

mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);

super.onCreate(savedInstanceState);

}


public void night() {

WindowManager.LayoutParams lp = new WindowManager.LayoutParams(

LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,

WindowManager.LayoutParams.TYPE_APPLICATION,

WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE

| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,

PixelFormat.TRANSLUCENT);


lp.gravity = Gravity.BOTTOM;// 可以自定义显示的位置

lp.y = 10;

if (mNightView == null) {

mNightView = new TextView(this);

mNightView.setBackgroundColor(0x80000000);

}

try{

mWindowManager.addView(mNightView, lp);

}catch(Exception ex){}


}

public void day(){

try{

mWindowManager.removeView(mNightView);

}catch(Exception ex){}

}

@Override

protected void onResume() {

if(CommonClass.IsNight){

night();

}else{

day();

}

super.onResume();


}

@Override

protected void onDestroy() {

super.onDestroy();

day();

}




        android:fillAfter="true" >
            android:duration="300"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toAlpha="0.4" />



        android:fillAfter="true" >
            android:duration="300"
        android:fromAlpha="0.4"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toAlpha="0.0" />


你可能感兴趣的:(Android)