Live update Screen Brightness with SeekBar - java

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
I have a seekbar in my preferences to update screen brightness. Is there a way to refresh the UI to see the live change?

I solved the problem with the following code it now works as intended!
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
BackLightValue = (float)arg1/100;
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
int SysBackLightValue = (int)(BackLightValue * 255);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
}

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);
}
});
}

Related

Set new image size only works when image is moved

I want to resize the image (overlayButton) when I click on my "ok" button. However, I don't know what to put in my setSize () method. I tried "overlayButton.setScaleX(size)" and "overlayButton.setScaleY(size)" but that does nothing. Here is my code. As it is, my image resizes but only once I have dragged it.
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekTxt.setText(Integer.toString(progress) + "%");
overlayButton.getLayoutParams().width = progress;
overlayButton.getLayoutParams().height = progress;
overlayButton.requestLayout();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
seekDialog.show();
}
}
public void sizeOK(View view) {
if (overlayButton != null) {
setSize(size);
seekDialog.dismiss();
} else {
seekDialog.dismiss();
}
}
public int getSize() {
return Math.round((float) size / 300 * 300);
}
public void setSize(int newSize) {
size = Math.round((float) newSize / 300 * 300);
overlayButton.
}
I'd be grateful for a pointer as to what to put in that setSize() method. Thanks!
Edit with solution:
The drawable needed to be refreshed. The solution is therefore:
public void sizeOK(View view) {
if (overlayButton != null) {
setSize(size);
WindowManager.LayoutParams params = (WindowManager.LayoutParams) overlayButton.getLayoutParams();
wm.updateViewLayout(overlayButton, params);
seekDialog.dismiss();
} else {
seekDialog.dismiss();
}
}

How To Using Image Adjustment (Brightness, Contrast) in UVCCamera Library on Android Studio

I'm trying to build an apps to capture picture from usb camera, using UVCCamera from https://github.com/saki4510t/UVCCamera
But, i didn't know, how to implement image adjustment setting (like Adjust Brightness, Contrast, White Balance) in this library.
I've tried to using seekbar to adjust brightness setting, and this is my code :
final UVCCamera camera = new UVCCamera();
private final OnSeekBarChangeListener mSeekBarChangeListener = new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if (mCameraHandler.isOpened()) //When USB Camera, Connected
{
camera.setBrightness(progress);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
};
And, if i try to change value of seekbar, the value has changed, but it doesn't change the brightness level.
Can anybody explain me, how to change the image adjustment in this library or give me correction about my code?
Any answers will be apreciate from me
Regards, and have a good day everyone :)
I updated the files in my project from the new version of the library (libuvccamera, usbCameraCommon) and modified the code from the example 8. I gave a sample code. Here is an example of the code that I got.
private SeekBar.OnSeekBarChangeListener seekBarChangeListener =
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
br = progress;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (isActive()) {
setValue(seekBar.getProgress());
}
}
// TODO Auto-generated method stub
};
private int setValue( final int value) {
return mCameraHandler != null ? mCameraHandler.setValue(value) : 0;
}
private boolean isActive() {
return mCameraHandler != null && mCameraHandler.isOpened();
}
And edit AbstractUVCCameraHandler.java
public int setValue( final int value) {
checkReleased();
final CameraThread thread = mWeakThread.get();
final UVCCamera camera = thread != null ? thread.mUVCCamera : null;
if (camera != null) {
camera.setBrightness(value);
return camera.getBrightness();
}
throw new IllegalStateException();
}

Android Spinner to change text size doesn't save state

private void onTextSizeSeekBarChange() {
final TextView tutorialText = (TextView) findViewById(R.id.tutorialText);
final SeekBar sb = (SeekBar) findViewById(R.id.seekBar);
sb.setMax(20);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int p = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
p = progress;
tutorialText.setTextSize(p);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (p < 20) {
sb.setProgress(p);
}
}
});
}
So the problem is that the code I have above changes the text size of a text view. However when going back and forth the state isn't saved so the text size is then reset. I am really new to android and would appreciate any guidance or help on how I can achieve this. I basically have a tutorial app and the text needs to change from the settings menu which has a seekbar allowing the user to select what text size they want. Any Ideas?
You can do either in this way :
1)you can save the value of p in shared pref and when you come next time you will check the value of p and set it on textview
2)you can save the value in onSavedInstance,but the problem is that when user kill the app from background you are going to loose the value
if you have set sb.setMax(20); then you dose not need to
if (p < 20) {
sb.setProgress(p);
}
code is useless user cant seek more then 20
you have code like that
private void onTextSizeSeekBarChange() {
final TextView tutorialText = (TextView) findViewById(R.id.tutorialText);
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(CONTEXT);
int p = SP.getInt("key", p);
tutorialText.setTextSize(p);
final SeekBar sb = (SeekBar) findViewById(R.id.seekBar);
sb.setMax(20);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
p = progress;
tutorialText.setTextSize(p);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
sb.setProgress(p);
Editor edit = SP.edit();
edit.putInt("key", p);
edit.commit();
}
});
}

Camera flash blinking stalls app

I want to add a new feature to my app: I created a progress bar and every time the progress changes, the flash will blink with a delay equal to the value of the progress in milliseconds.
When I modify the progress, the flash begins to blink but I can't do anything until it reaches the number of blinks (20). I want to be able to modify the delay even if the flash is still blinking.
private void blink(int sleepMS) throws Exception{
//int sleepMS=(1/10)*50;
int flashCount=20;
getCamera();
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
Thread thr = new Thread();
thr.start();
for(int i=0;i<flashCount;i++) {
flipFlash();
thr.sleep(sleepMS);
flipFlash();
thr.sleep(sleepMS);
}
camera.stopPreview();
//camera.release();
}
private void flipFlash(){
if (isFlashOn) {
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
isFlashOn = false;
} else{
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
isFlashOn = true;
}
}
Where getCamera() gets camera parameters. And the code for the SeekBar listener:
volumeControl = (SeekBar) findViewById(R.id.volume_bar);
volumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressChanged = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
try {
blink(100 - progressChanged);
} catch (Exception e) {
}
}
});
Because your for loop is on you main thread it is blocking all other operations. From you code, it seems like you meant to do something like this:
Thread thr = new Thread(new Runnable() {
#Override
public void run() {
for(int i=0;i<flashCount;i++) {
flipFlash();
thr.sleep(sleepMS);
flipFlash();
thr.sleep(sleepMS);
}
}
});
thr.start();

Seekbar's onProgreesChanged not working correctly when dragged to quickly

I've created a DJ program with a mediaplayer on each side of the screen(like in the picture below)
The seekbar between those two whenever moved should increase volume of one mediaplayer and decrease the other mediaplayer's volume respectively. When I drag it very slowly it works perfectly, the problem is when I move it faster.
When dragged faster the volumes doesn't always decrease to the end or doesn't decrease at all, at super speed the volume doesn't change at all.
Here is the code:
private void initiateSeekBar()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.seekBar);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//volumeSeekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
//volumeSeekbar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
final int initVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
final int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,maxVolume, 0);
volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
float volume = (float) (1 - (Math.log(100 - progress) / Math.log(100)));
//audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
if (volume <= 0.05) {
volume = 0;
}
System.out.println("\nVolume: \n" + volume);
if (mediaplayerLeft != null) {
mediaplayerLeft.setVolume(1 -volume, 1 - volume);
}
if (mediaplayerRight != null) {
mediaplayerRight.setVolume(volume, volume);
}
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
Why is that happening? Is that how seekbar works or is it a problem with my code?

Categories

Resources