实时更新屏幕亮度与搜索栏 [英] Live update Screen Brightness with SeekBar
问题描述
所以,我有code表示调整系统的亮度和我正确实施了搜索栏,但是当我调整搜索栏的屏幕亮度不会改变。我看着其他几个职位及解决方案似乎是创建打开和真的很快关闭一个虚拟类。如果我这样做,虽然后来我觉得会有一吨的滞后,因为屏幕会不停刷新的搜索栏移动。
@覆盖
公共无效onProgressChanged(搜索栏搜索栏,INT进步,布尔FROMUSER){
ContentResolver的CR = getContentResolver();
尝试{
INT亮度= Settings.System.getInt(CR,Settings.System.SCREEN_BRIGHTNESS);
Settings.System.putInt(CR,Settings.System.SCREEN_BRIGHTNESS,亮度);
WindowManager.LayoutParams LP = getWindow()的getAttributes()。
lp.screenBrightness =亮度/ 255.0f;
。getWindow()setAttributes(LP);
}
赶上(Settings.SettingNotFoundException一)
{
}
}
@覆盖
公共无效onStartTrackingTouch(搜索栏搜索栏){
//Toast.makeText(MainActivity.this,开始跟踪搜索栏,Toast.LENGTH_SHORT).show();
}
@覆盖
公共无效onStopTrackingTouch(搜索栏搜索栏){
}
还有另一个问题,刷新的是,我遇到这个亮度控制在一个屏幕,在这里有一个摄像头preVIEW每一次的伪屏幕负荷会需要一些时间,进一步提高滞后杀相机P>
我没有看到这个帖子,但我不能想出如何实现它。因为我没有使用preferences
<一个href="http://stackoverflow.com/questions/9023926/i-have-a-seekbar-in-my-$p$pferences-to-update-screen-brightness-is-there-a-way-t">I在我的preferences一个搜索栏来更新屏幕亮度。有没有一种方法来刷新UI看直播变化?
使用我下面code它完美地运行:
在你的任何XML文件:
&LT;搜索栏
机器人:ID =@ + ID / seekBar1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =中心
机器人:layout_weight =1
机器人:背景=机器人:ATTR / selectableItemBackground
机器人:最大=10
机器人:progressDrawable =@可绘制/进步
机器人:scaleType =fitCenter
机器人:拇指=@可绘制/拇指/&GT;
请注意:需要的属性尽可能多的,你需要在搜索栏
。在你的任何活动中使用下面的方法,并调用:
私人无效setSeekbar()
{
搜索栏=(搜索栏)findViewById(R.id.seekBar1);
seekBar.setMax(255);
浮curBrightnessValue = 0;
尝试 {
curBrightnessValue = android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS);
}赶上(SettingNotFoundException E){
e.printStackTrace();
}
INT screen_brightness =(INT)curBrightnessValue;
seekBar.setProgress(screen_brightness);
seekBar.setOnSeekBarChangeListener(新OnSeekBarChangeListener(){
INT进度= 0;
@覆盖
公共无效onProgressChanged(搜索栏搜索栏,INT progresValue,
布尔FROMUSER){
进度= progresValue;
}
@覆盖
公共无效onStartTrackingTouch(搜索栏搜索栏){
}
@覆盖
公共无效onStopTrackingTouch(搜索栏搜索栏){
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
进展);
}
});
}
So I have code that adjusts System Brightness and I have properly implemented the seek bar but when I adjust the seekbar the Screen Brightness does not change. I looked into a few other posts and the solution seems to be to create a Dummy class that opens and closes really quickly. If I do that though then I feel like there would be a ton of lag because the screen would be refreshing constantly as the seekbar is moving.
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
ContentResolver cr = getContentResolver();
try{
int brightness = Settings.System.getInt(cr,Settings.System.SCREEN_BRIGHTNESS);
Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, brightness);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness / 255.0f;
getWindow().setAttributes(lp);
}
catch (Settings.SettingNotFoundException a)
{
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//Toast.makeText(MainActivity.this, "Started Tracking Seekbar", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
Also another problem with the refresh is that I am running this brightness control in a screen where there is a camera preview and killing the camera each time the dummy screen loads would take some time and further increase the lag
I did see this post but I couldnt figure out how to implement it. Since I am not using preferences
Use my following code it perfectly running :
in your whatever xml file :
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground"
android:max="10"
android:progressDrawable="@drawable/progress"
android:scaleType="fitCenter"
android:thumb="@drawable/thumb" />
note : take attributes as much you need in seekbar.
in your whatever activity use following method and call that :
private void setSeekbar()
{
seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setMax(255);
float curBrightnessValue = 0;
try {
curBrightnessValue = android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
int screen_brightness = (int) curBrightnessValue;
seekBar.setProgress(screen_brightness);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
progress = progresValue;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
progress);
}
});
}
这篇关于实时更新屏幕亮度与搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!