问题描述
我要根据光线。要改变屏幕亮度
I want to change brightness of screen according to light..
我找到的链接获取亮度和设置亮度。
I found links for getting brightness level and setting up brightness..
Change屏幕亮度的onPause(Android应用程序)
How切换自动亮度和关闭? (不重复)
changing屏幕亮度以编程方式在android的
Changing屏幕亮度编程(与电源部件)
不过,我想改变亮度的根据周围的光线 ..总之,我想做的事情一样自动亮度工程..
But I want to change brightness as per surrounding light..In short, I want to do same as auto brightness works..
我如何检测周围光线的?或者我可以触发自动亮度功能??
How Can I detect surrounding light?? OR Can I trigger auto brightness functionality??
亮度是有值的范围从0到255。我想设置亮度级别,根据周围的光照强度不能为0到255我怎样才能得到这两者之间的比值。
brightness is having value range between 0 to 255.I want to set brightness level according to surrounding light level which can not be 0 to 255. How can I get ratio between this two.
帮助是必要的。谢谢
推荐答案
这是我自己的解决方案:
Solution from myself:
我实现了从here.我用aboce链接也让它完成。
I implemented shaking listener from here.I used aboce links also to make it completed.
我在 ShakeBrightService.java
public class ShakeBrightService extends Service {
private ShakeDetector shakeBrightDetector = new ShakeDetector();
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
shakeBrightDetector.stop();
shakeBrightDetector.start(this, shakeBrightEventListener);
return super.onStartCommand(intent, flags, startId);
}
private ShakeEventListener shakeBrightEventListener = new ShakeEventListener() {
@Override
public void onShake() {
setAutoBrightness(true);
}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void setAutoBrightness(boolean value) {
Toast.makeText(getApplicationContext(), "AutoBrightness : " + value,
Toast.LENGTH_SHORT).show();
if (value) {
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
} else {
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
Intent i = new Intent(getApplicationContext(), MainActivity1.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
}
假的令人耳目一新的观点
MainActivity1.java
Fake for refreshing view
MainActivity1.java
public class MainActivity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main1);
refreshBrightness(getBrightnessLevel());
Thread t = new Thread() {
public void run() {
try {
sleep(10);
} catch (InterruptedException e) {
}
finish();
}
};
t.start();
}
private void refreshBrightness(float brightness) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
if (brightness < 0) {
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
} else {
lp.screenBrightness = brightness;
}
getWindow().setAttributes(lp);
}
int getBrightnessLevel() {
try {
int value = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS);
// convert brightness level to range 0..1
value = value / 255;
return value;
} catch (SettingNotFoundException e) {
return 0;
}
}
FakeActivity是有主题安卓主题=@安卓风格/ Theme.Translucent.NoTitleBar
,于是默默亮度改变
FakeActivity is having theme android:theme="@android:style/Theme.Translucent.NoTitleBar"
, so silently brightness is changed.
这篇关于根据机器人周围光线亮度变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!