增加屏幕亮度以进行活动 [英] Increasing screen brightness for activity

查看:99
本文介绍了增加屏幕亮度以进行活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,在Android OS中,至少有三种不同的技术可用于更改屏幕亮度.他们中的两个不再在杯形蛋糕后工作,而第三个被接受的技术显然有一个错误.

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.

我所使用的使用WindowManager.LayoutParams的当前技术的示例范围为2行代码

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?

推荐答案

您可以做的简单的事情就是使用拖放操作在xml中创建SeekBar或编写其xml

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" />

在mainActivity类中执行以下操作:

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

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

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" />

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

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

    }
}

这篇关于增加屏幕亮度以进行活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆