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

该博客介绍如何在Android应用中通过添加半透明蒙版来实现夜间模式。通过创建一个TextView并设置布局参数,然后利用WindowManager动态添加和移除视图,实现了夜间模式的开关。此外,还提供了两个XML动画文件,分别用于渐变显示和隐藏蒙版,实现了平滑过渡的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}



<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true" >
    <alpha
        android:duration="300"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toAlpha="0.4" />
</set>


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true" >
    <alpha
        android:duration="300"
        android:fromAlpha="0.4"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toAlpha="0.0" />
</set>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值