我的应用程序是一个简单的“亮度变换器”,它也会改变系统亮度(不容易实现的东西:D)。
我实际上设置了所有的东西,它的工作正常...除了一个小烦人的事情......每次亮度改变屏幕首先“闪烁”到最高亮度(如100%光)和半秒左右后,它改变正确的亮度。
示例:系统亮度应为120:1。更改为255(最大),更改半秒更改为120.
这是我的两个班级的代码。 Dummy类用于“重置”(应用亮度变化所必需的。)
编辑:顺便说一句。我认为问题是“lp.screenBrightness = 100 / 100.0f;”在虚拟活动中。但没有它它就无法工作:/test.java
public class Test extends Activity
{
SeekBar seekbar;
TextView value;
TextView debug;
Button button1;
Button button2;
Button button3;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
value = (TextView) findViewById(R.id.textview2);
debug = (TextView) findViewById(R.id.textview3);
seekbar = (SeekBar) findViewById(R.id.seekbar);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
seekbar.setOnSeekBarChangeListener( new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
// TODO Auto-generated method stub
int brightness = progress+1;
double b = brightness/2.55;
double c = Math.round(b*100)/100;
int percent = (int) c;
if (percent==0) {
value.setText("Lightlevel: !MIN!");
}else if (percent==100){
value.setText("Lightlevel: !MAX!");
}else{
value.setText("Lightlevel: "+percent+"%");
}
debug.setText("int brightness: "+brightness+" / double b:"+b+" / double c: "+c+" / int percent: "+percent);
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightness);
}
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
Intent in = new Intent(Test.this,DummyBrightnessActivity.class);
startActivity(in);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, 1);
Intent in = new Intent(Test.this,DummyBrightnessActivity.class);
startActivity(in);
}
});
button2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, 127);
Intent in = new Intent(Test.this,DummyBrightnessActivity.class);
startActivity(in);
}
});
button3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, 255);
Intent in = new Intent(Test.this,DummyBrightnessActivity.class);
startActivity(in);
}
});
}
}
DummyBrightnessActivity.java
public class DummyBrightnessActivity extends Activity{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.dummy);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100/100.0f;
getWindow().setAttributes(lp);
Timer timer2 = new Timer();
timer2.schedule(new TimerTask() {
public void run() {
finish();
}
}, 500);
}
}
答案 0 :(得分:0)
回答我自己的问题以备将来使用。
问题实际上是lp.screenBrightness = 100/100.0f;
。所以我在intent
中添加了一个具有brightness
正确值的变量,所以我可以将它变成lp.screenBrightness = brightness;
。
我知道这有点短,但如果有人想了解更多信息,请发表评论。