增加活动的屏幕亮度

发布于 2024-09-03 03:39:10 字数 447 浏览 5 评论 0原文

显然,Android 操作系统中至少有三种不同的技术可以改变屏幕亮度。其中两个在纸杯蛋糕之后不再起作用,而第三个被接受的技术显然有一个错误。

我想在单视图活动开始时增加屏幕亮度,然后在活动结束时将亮度恢复为用户设置。没有按钮,没有第二个视图或第二个活动。只是在开始时达到最大亮度,并在活动结束时返回到原始亮度设置。

我找到的使用 WindowManager.LayoutParams 的当前技术的示例范围从 2 行代码

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);

到几页源代码和大量活动。

是否有一个在开始时最大化屏幕亮度的简单示例,有人可以提供链接?

There are apparently at least three different techniques for changing screen brightness in the Android OS. Two of them no longer work post-cupcake and the third accepted technique evidently has a bug.

I would like to increase screen brightness at the start of a one-view activity then turn the brightness back to the user setting at the end of the activity. No buttons, no second view or second activity. Just a maxed brightness at start and a return to original brightness setting at the end of the activity.

The examples of the current technique using WindowManager.LayoutParams that I have located range from 2 lines of code

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);

to several pages of source code and numerous activities.

Is there a simple example of maxing the screen brightness at start that someone can provide a link to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南巷近海 2024-09-10 03:39:10

您可以做的简单的事情是使用拖放在 xml 中创建一个 SeekBar 或

 SeekBar
     android:id="@+id/brightnesSseeker"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:max="255"
     android:progress="255" />

在 mainActivity 类中编写其 xml 执行以下操作:

brightnesSseeker = (SeekBar) findViewById(R.id.brightnesSseeker);

    brightnesSseeker
            .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // Nothing handled here
                }

                public void onStartTrackingTouch(SeekBar seekBar) {
                    // Nothing handled here
                }

                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean fromUser) {
                    // Set the minimal brightness level
                    // if seek bar is 20 or any value below
                    if (progress <= 20) {
                        // Set the brightness to 20
                        screenBrightness(20);
                    } else {
                        // Set brightness variable based on the progress bar
                        screenBrightness(progress);
                    }
                    stobeVariable=progress*10;
                }
            });

然后编写一个简单的函数:

private void screenBrightness(double newBrightnessValue) {
    /*
     * WindowManager.LayoutParams settings = getWindow().getAttributes();
     * settings.screenBrightness = newBrightnessValue;
     * getWindow().setAttributes(settings);
     */
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    float newBrightness = (float) newBrightnessValue;
    lp.screenBrightness = newBrightness / (float) 255;
    getWindow().setAttributes(lp);
}

还要在调用 screenBrightness 函数之前检查亮度模式,因为有时会选择自动模式亮度,在这种情况下亮度不会改变。您还需要清单文件中的权限。

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

在 onCreate 中 setOnSeekBarChangeListener 之前调用以下函数

  void changeBrightnessMode(){

    try {
        int brightnessMode = Settings.System.getInt(getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE);
        if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
            Settings.System.putInt(getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE,
                    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        }


    } catch (Exception e) {
        // do something useful

    }
}

Its simple what you can do is make a SeekBar in xml using drag drop or write its xml

 SeekBar
     android:id="@+id/brightnesSseeker"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:max="255"
     android:progress="255" />

in mainActivity class do this:

brightnesSseeker = (SeekBar) findViewById(R.id.brightnesSseeker);

    brightnesSseeker
            .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // Nothing handled here
                }

                public void onStartTrackingTouch(SeekBar seekBar) {
                    // Nothing handled here
                }

                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean fromUser) {
                    // Set the minimal brightness level
                    // if seek bar is 20 or any value below
                    if (progress <= 20) {
                        // Set the brightness to 20
                        screenBrightness(20);
                    } else {
                        // Set brightness variable based on the progress bar
                        screenBrightness(progress);
                    }
                    stobeVariable=progress*10;
                }
            });

and then write a simple function :

private void screenBrightness(double newBrightnessValue) {
    /*
     * WindowManager.LayoutParams settings = getWindow().getAttributes();
     * settings.screenBrightness = newBrightnessValue;
     * getWindow().setAttributes(settings);
     */
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    float newBrightness = (float) newBrightnessValue;
    lp.screenBrightness = newBrightness / (float) 255;
    getWindow().setAttributes(lp);
}

Also do check the brightness mode before calling screenBrightness function because sometime the mode is selected to auto brightness and in that case the brightness won't change. You also need permission in manifest file too.

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

Call the function bellow in onCreate before setOnSeekBarChangeListener

  void changeBrightnessMode(){

    try {
        int brightnessMode = Settings.System.getInt(getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE);
        if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
            Settings.System.putInt(getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE,
                    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        }


    } catch (Exception e) {
        // do something useful

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文