如何在单击按钮时增加或减少屏幕亮度
how to incrase or decrease the brighness of the screen on button click
在我的 android 应用程序中,我想要一个按钮,其中
第一次点击亮度将为 10 ,
第二次点击亮度将是 50 ,
第三次点击亮度将为 255(全)
当用户点击下一步时,它将再次回到 10
如何编码这个我已经厌倦了这个解决方案但只能增加
private void getBrightness() {
try
{ int curBrightnessValue = 0 ;
curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
if(curBrightnessValue >= 250 )
{
int SysBackLightValue = curBrightnessValue-30;
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
curBrightnessValue=SysBackLightValue;
}
else if(curBrightnessValue <250)
{
int SysBackLightValue = curBrightnessValue+30;
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
curBrightnessValue=SysBackLightValue;
}
请帮我解决这个问题
你的代码甚至没有按照你在问题中所说的那样做(你只是尝试增加和减少 30 而没有应用它),你正在以正确的方式获得亮度,而你所要做的就是要做的是处理按钮点击监听器中的新值:
private int getBrightness() {
int curBrightnessValue = 0 ;
try
{
curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
}
catch(Exception ex){
curBrightnessValue = 0
}
return curBrightnessValue;
}
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int current_brightness = getBrightness();
int new_brightness;
if(current_brightness > 250)
new_brightness = 10;
else if(current_brightness >= 50)
new_brightness = 255;
else if(current_brightness >= 10)
new_brightness = 50;
else
new_brightness = 150; // let's say this will be the default value
// and to set the brightness
set_system_brightness(new_brightness);
}
});
private void set_system_brightness(int new_brightness){
Settings.System
.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS,
new_brightness); // Set the system
// brightness
android.view.WindowManager.LayoutParams w_lp = getWindow()
.getAttributes(); // Get the current window
// attributes
w_lp.screenBrightness = new_brightness / (float) 255; // Set
// the
// brightness
// of
// this
// window
getWindow().setAttributes(w_lp); // Apply attribute
// changes to
// this window
}
在我的 android 应用程序中,我想要一个按钮,其中 第一次点击亮度将为 10 , 第二次点击亮度将是 50 , 第三次点击亮度将为 255(全) 当用户点击下一步时,它将再次回到 10 如何编码这个我已经厌倦了这个解决方案但只能增加
private void getBrightness() {
try
{ int curBrightnessValue = 0 ;
curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
if(curBrightnessValue >= 250 )
{
int SysBackLightValue = curBrightnessValue-30;
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
curBrightnessValue=SysBackLightValue;
}
else if(curBrightnessValue <250)
{
int SysBackLightValue = curBrightnessValue+30;
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
curBrightnessValue=SysBackLightValue;
}
请帮我解决这个问题
你的代码甚至没有按照你在问题中所说的那样做(你只是尝试增加和减少 30 而没有应用它),你正在以正确的方式获得亮度,而你所要做的就是要做的是处理按钮点击监听器中的新值:
private int getBrightness() {
int curBrightnessValue = 0 ;
try
{
curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
}
catch(Exception ex){
curBrightnessValue = 0
}
return curBrightnessValue;
}
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int current_brightness = getBrightness();
int new_brightness;
if(current_brightness > 250)
new_brightness = 10;
else if(current_brightness >= 50)
new_brightness = 255;
else if(current_brightness >= 10)
new_brightness = 50;
else
new_brightness = 150; // let's say this will be the default value
// and to set the brightness
set_system_brightness(new_brightness);
}
});
private void set_system_brightness(int new_brightness){
Settings.System
.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS,
new_brightness); // Set the system
// brightness
android.view.WindowManager.LayoutParams w_lp = getWindow()
.getAttributes(); // Get the current window
// attributes
w_lp.screenBrightness = new_brightness / (float) 255; // Set
// the
// brightness
// of
// this
// window
getWindow().setAttributes(w_lp); // Apply attribute
// changes to
// this window
}