Increment/Decrement SeekBar Slowly - java

I'm trying to use a button to get my SeekBar to slowly increment && || decrement to a certain int value (maybe I can only do this with a double?), let's say the progress of the seekbar starts at 0, I want to increment to 50 over about a 5 second period of time. Is this possible in Android?
// ON SEEKBAR CHANGE
fill.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
fillProgress = String.valueOf(progress);
fillView.setText(fillProgress);
}
});
centerDistribution.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TOAST: ARE YOU SURE YOU WANT TO fill bucket?
// IF YES
// INCREMENT SEEKBAR SLOWLY
}
});

I've never personally used it for this purpose but you can use an AsyncTask to make sure your UI remains responsive during the moving process. Essentially you'll use AsyncTask like you were updating a progress bar, but instead of a progress bar you'll be updating the value of the seekbar instead. You'll need to override the entire class, but the bread and butter will be here:
private class AsyncTaskSeekbar extends AsyncTask<String, String, String>
{
#Override
protected void doInBackground(String... params)
{
try
{
int time = 5000; //5000 ms = 5 seconds
int incTime = 5000 / 50; //we want it to get to 50 after 5 seconds 100ms per Seekbar unit
for(int i = 0; i < time; i += incTime)
{
Thread.sleep(incTime);
publishProgress(); // Calls onProgressUpdate()
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected void onProgressUpdate()
{
seekBar.setProgess(seekBar.getProgess() + 1);
}
}
Of course you'll have to do some things like initialize the AsyncTaskSeekbar, pass in values dynamically, etc. But this should give you a good idea of how to get started.

Related

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

Exit activity after killing all threads android

I am trying to play with progress bars. I have this (below) simple activity which runs a progress bar N times one after the other, when I call Progress(N). It is working great but the problem I am facing is, if I press back button. I get into the mainActivity but the progress bars (the threads) are still running in background one after the other. As soon as they finish N loops, the intent is called and whatever I would be doing would be interrupted by this LOOP_OVER activity.
I tried solving this by my own. I tried using variable of Thread class (before I was directly doing it). And tried to interrupt() it at onDestroy() or even just before the intent is called but its not helping. How should I go about it?
public class Loop extends Activity {
private ProgressBar progressBar;
private CircleProgress circleProgress;
private int progressStatus = 0;
private Handler handler = new Handler();
private TextView myView;
private int started = 0, doneLoop=0;
private Thread th;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loop);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
circleProgress = (CircleProgress) findViewById(R.id.circle_progress);
myView = (TextView) findViewById(R.id.instruction);
progressBar.setScaleY(3f);
// Start long running operation in a background thread
Progress(3);
}
#Override
public void onDestroy() {
// Below, everything I am just
th.interrupt();
Loop.this.finish();
Thread.currentThread().interrupt();
super.onDestroy();
}
public void Progress(final int numberOfRuns){
// QueView.setText(Que);
if(numberOfRuns == 0){
th.interrupt();
Intent myIntent = new Intent(Loop.this, LOOP_OVER.class);
startActivity(myIntent);
super.onDestroy();
finish();
}
th = new Thread(new Runnable() {
public void run() {
genNextSet();
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
circleProgress.setProgress(progressStatus);
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
myView.setText(Que);
}
});
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressStatus = 0;
Progress(numberOfRuns - 1);
}
});
th.start();
}
private void genNextSet() {
// so some cool here!
}
}
You can think of a class variable that is shared among all threads.
Try to add something like this:
private Boolean LOOP = true;
then
while (progressStatus < 100 && LOOP) {
and
#Override
public void onBackPressed() {
LOOP = false
}
also
if(LOOP == true){
// call intent
}
finish();
Your activity does not get destroyed, if you press the "Back"-key, thus onDestroy() will not be called.I'd override onBackPressed(), if I where you.Alternatively, you could try to put it into the onPause()-method.
You haven't override the back button pressed..try this
#Override
public void onBackPressed() {
th.interrupt();
Loop.this.finish();
Thread.currentThread().interrupt();
super.onBackPressed();
// add finish() if you want to kill current activity
}

ProgressBar with background images [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to create a progress barProgressBar screenshot like given image where the star image should reduce based on time(every second). So,Please guide me how to do? I am using the handler for countdown timer
timeString* we are getting it from different class
String timestring = getIntent().getStringExtra("ageintent");
timereceived = Integer.parseInt(timestring);
seekbar.setMax(timereceived);
seekbar.setClickable(false);
timereceived--;
time.setText(String.valueOf(timereceived));
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
handler = new Handler();
run = new Runnable() {
#Override
public void run() {
int seconds = Integer.parseInt(timeseconds.getText().toString());
int timechange = Integer.parseInt(time.getText().toString());
seekbar.setProgress(seconds);
seconds--;
timeseconds.setText(String.valueOf(seconds));
if (seconds == 0 && timechange == 0) {
Toast.makeText(Naughty_Step.this, "Your punishment is completed", Toast.LENGTH_SHORT).show();
soundplayer.start();
timereceived--;
} else if (seconds == 0) {
timeseconds.setText("59");
timechange--;
seekbar.setProgress(seconds);
time.setText(String.valueOf(timechange));
// int min= Integer.parseInt(time.getText().toString());
// seekbar.setProgress(min);
handler.postDelayed(this, 1000);
} else {
handler.postDelayed(this, 1000);
}
}
};
handler.postDelayed(run, 1000);
}
Try below -
SeekBar seekBar1 = (SeekBar)findViewById(R.id.seekBar1);
drw = getResources().getDrawable(R.drawable.ic_launcher);
seekBar1.setOnSeekBarChangeListener(new yourListener());
private class yourListener implements SeekBar.OnSeekBarChangeListener {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
iSize = 100 - progress;
tv.setText(String.valueOf(new Integer(progress)));
drw = resize(drw);
seekBar.setThumb(drw);
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
}
//Reduce size of seekbar
private Drawable resize(Drawable image) {
Bitmap b = ((BitmapDrawable)image).getBitmap();
Bitmap bitmapResized = Bitmap.createScaledBitmap(b, iSize, iSize, false);
return new BitmapDrawable(getResources(), bitmapResized);
}
Add this as inner class of your activity or fragment need progressbar reduce.
class CounterClass extends CountDownTimer {
int totalDuration;
public CounterClass(long millisInFeature, long countDownInterval) {
// TODO Auto-generated constructor stub
super(millisInFeature , countDownInterval);
this.totalDuration =millisInFeature
}
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
int progress =((millisUntilFinished * 100 / totalDuration))
progressbar.setProgress(progress);
}
#Override
public void onFinish() {
// TODO Auto-generated method stub
}
oncreate() or event start
(new CounterClass(30000,1000)).start();

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

Changing TextView every 5 seconds

In my strings.xml I have 5 strings with text. And I want to be able to change the content of a TextView every 5 seconds.
Example:
First 5 seconds the TextView will show the content of the first string, then next five seconds it will show the second string. And after the fifth string it will show the first string again.
Sorry about this bad description, I'm new in Java.
Try this:
final TextView textView = yourTextView;
final int[] array = {R.string.text1, R.string.text2,R.string.text3,R.string.text4,R.string.text5};
textView.post(new Runnable() {
int i = 0;
#Override
public void run() {
textView.setText(array[i]);
i++;
if (i ==5)
i = 0;
textView.postDelayed(this, 5000);
}
});
new CountDownTimer(25000, 5000) {
public void onTick(long millisUntilFinished) {
mTextField.setText(//question here);
}
public void onFinish() {
mTextField.setText(//End of questions);
}
}.start();
For further Info see this -> http://developer.android.com/reference/android/os/CountDownTimer.html
You can use "Handler" in this
Create a handler in onCreate
`String[] arr = {R.string.value1, R.string.value2,R.string.value3,R.string.value4,R.string.value5};`
Handler mHandler = new Handler();
Now create one Thread and use while loop to periodically perform the task using the sleep method of the thread.
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<5;i++){
try {
Thread.sleep(5000);
mHandler.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
textView.setText(arr[i])
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();

Categories

Resources