So I am trying to get a SeekBar to function like a gas pedal.
When it is at the neutral postion (progress = 50) then my value does not change. When the SeekBar progress becomes greater than or less than 50, I take that difference from 50 and use it to calculate a value that gets pushed to a TextView.
This addition or subtraction to the value does not occur because the progress has changed but instead based on how long it is held at that point of progress. THis means that the farther from 50 you are, the faster the value goes up, or in the other direction, down.
My first thought was a Thread that had a while loop that slept and then updated the value. I stored the progress of the SeekBar globally. This worked for calculating my value, but because it doesn’t run on the UI thread I can’t update the UI from it.
How could I make this “Gas Pedal” SeekBar work?
This could all run in the UI thread. Use
postDelayedto run a Handler to update your “speedometer” once in a while (requesting 250 ms seems OK). However often your Handler actually runs, you’ll use actual time elapsed, not the requested interval, to calculate your speedometer update. (You have to do that in any physics simulation because the real world keeps running no matter how often or how irregularly your update functions get called.)