如何改变亮度?

时间:2015-09-21 12:48:56

标签: android screen-brightness

我运行Runnable,它应该逐渐改变亮度

private Runnable runnable = new Runnable(){

        @Override
        public void run(){
            if(intensity < 1){ intensity += intensityGrow; }
            if(intensity > 1){ intensity = 1f; }
            Log.e("intensity", intensity + "/grow "+intensityGrow);

            android.provider.Settings.System.putInt(
                getContentResolver(),

                android.provider.Settings.System.SCREEN_BRIGHTNESS,(int)(255*intensity)
            );
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

            am.setStreamVolume(
                AudioManager.STREAM_ALARM, (int) (am.getStreamMaxVolume(AudioManager.STREAM_ALARM) * intensity), 0
            );
            if(intensity < 1){
                Log.e("intensity", "continue");

                handler.postDelayed(runnable, 1000);
            }

        }
    };

我也尝试使用

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = intensity;
        getWindow().setAttributes(lp);

Manifest中有<uses-permission android:name="android.permission.WRITE_SETTINGS"/> 也没有结果。

2 个答案:

答案 0 :(得分:0)

试试这个,

new Thread() {
    public void run() {
        for (int i = initial; i < target; i++) {
            final int bright = i;
            handle.post(new Runnable() {
                public void run() {
                    float currentBright = bright / 100f;
                    window.getAttributes().screenBrightness = currentBright;
                    window.setAttributes(window.getAttributes());
                });
            }
            try {
                sleep(step);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}.start();

答案 1 :(得分:0)

首先要将此功能设置为nessesarry,将亮度模式设置为手动

Settings.System.putInt(
                        getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
                    );

然后使用两种方法改变亮度

android.provider.Settings.System.putInt(
                getContentResolver(),

                android.provider.Settings.System.SCREEN_BRIGHTNESS, (int) (255 * intensity)
            );
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.screenBrightness = intensity;
            getWindow().setAttributes(lp);