Android 护眼模式的实现

思路获取content根布局,在上面添加一层浮层,默认透明,开启护眼模式设置护眼色值。

实现在BaseActivity的onCreate方法中,添加浮层,所有的Activity继承BaseActivity即可

步骤1:

 /**
     * 添加护眼模式浮层
     */
    protected void initEye() {
        content = ((ViewGroup) findViewById(android.R.id.content));
        view = new FrameLayout(this);
        view.setBackgroundColor(Color.TRANSPARENT);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
//        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        content.addView(view, params);
    }

/**
     * 开启护眼模式
     */
    public void openEye() {
        view.setBackgroundColor(getFilterColor(30));
    }

    /**
     * 关闭护眼模式
     */
    public void closeEye() {
        view.setBackgroundColor(Color.TRANSPARENT);
    }

    /**
     * 过滤蓝光
     *
     * @param blueFilterPercent 蓝光过滤比例[10-30-80]
     */
    public int getFilterColor(int blueFilterPercent) {
        int realFilter = blueFilterPercent;
        if (realFilter < 10) {
            realFilter = 10;
        } else if (realFilter > 80) {
            realFilter = 80;
        }
        int a = (int) (realFilter / 80f * 180);
        int r = (int) (200 - (realFilter / 80f) * 190);
        int g = (int) (180 - (realFilter / 80f) * 170);
        int b = (int) (60 - realFilter / 80f * 60);
        return Color.argb(a, r, g, b);
    }

步骤2:在application中监听Activity


    /**
     * 初始化 activity 监听
     */
    private void initActivityCallBack() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                AppManager.getAppManager().addActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {
                if (activity instanceof BaseActivity) {
                    BaseActivity baseActivity = (BaseActivity) activity;
                    String type = StoreUtils.getString(getApplicationContext(), Constants.UESR_SET_EYE);
                    if (!TextUtils.isEmpty(type)) {
                        if (type.equals("0")) {
                            baseActivity.closeEye();
                        } else {
                            if (!(activity instanceof RemindDialogActivity) && !(activity instanceof RemindOpenDialogActivity)) {
                                baseActivity.openEye();
                            }
                        }
                    }
                }
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                if (activity != null) {
                    AppManager.getAppManager().finishActivity(activity);
                }
            }
        });

    }

在onActivityStarted中处理相应逻辑,在开启护眼模式的时候,调用BaseActivity中的方法开启,调整色值。

当activity是作为dialog时,要避免开启,因为dialog不覆盖浮层。

实现记录之!

你可能感兴趣的:(Android工作经验之谈)