简体   繁体   中英

How to animate seekbar thumb to move to a specific position

I defined different lock positions:

private static final int[] gears = new int[] { 0, 33, 66, 100 };

In the onStopTrackingTouch i calculate which of my lock positions is the closest to the progress state of my SeekBar :

@Override
public void onStopTrackingTouch(final SeekBar seekBar) {
    int progress = seekBar.getProgress();
    int distance = Math.abs(gears[0] - progress);
    int index = 0;

    for(int i = 1; i < gears.length; i++) {
        int tempDistance = Math.abs(gears[i] - progress);

        if(tempDistance < distance) {
            index = i;
            distance = tempDistance;
        }
    }

    int lock = gears[index];
}

I now need to somehow set the progress state of the SeekBar to my lock position using a smooth animation.

How can i impelement that animation?

EDIT

Thanks to the answer of @bwoogie here the complete working example using a VerticalSeekBar

@Override
public void onStopTrackingTouch(final SeekBar seekBar) {
    int progress = seekBar.getProgress();
    int distance = Math.abs(gears[0] - progress);
    int index = 0;

    for(int i = 1; i < gears.length; i++) {
        int tempDistance = Math.abs(gears[i] - progress);

        if(tempDistance < distance) {
            index = i;
            distance = tempDistance;
        }
    }

    final int lock = gears[index];
    final int tick = 50;
    final VerticalSeekBar verticalSeekBar = (VerticalSeekBar)seekBar;

    new Thread(new Runnable() {
        long now = System.currentTimeMillis();

        public void run() {
            while(seekBar.getProgress() != lock) {
                if(now + tick < System.currentTimeMillis()) {
                    if(seekBar.getProgress() > lock) {
                        getActivity().runOnUiThread(new Runnable() {
                            public void run() {
                                verticalSeekBar.setMax(100);
                                verticalSeekBar.setProgressAndThumb(seekBar.getProgress() - 1);
                            }
                        });

                    } else if(seekBar.getProgress() < lock) {
                        getActivity().runOnUiThread(new Runnable() {
                            public void run() {
                                verticalSeekBar.setMax(100);
                                verticalSeekBar.setProgressAndThumb(seekBar.getProgress() + 1);
                            }
                        });

                    } else {
                        break; 
                    }

                    now = System.currentTimeMillis();
                }
            }
        }
    }).start();
}

What I would do is create a loop (in a new thread) that checks every few milliseconds if enough time has passed and if so then we can update the seekBar. Here is the basic idea, just throw it into a thread and implement the updateSeekBar() method since its in a thread you wont be able to access the UI thread directly.

    int newPos; // the new position we are seeking to
    int ticks = 50; //how many milliseconds
    long now = System.getCurrentTimeMillis();

    while(seekBar.getPosition() != newPos) {
       if(now + ticks < System.getCurrentTimeMillis()) {
          if(seekBar.getPosition() > newPos) {
              updateSeekbar(seekBar.getPosition()--);}
          elseif(seekBar.getPosition() < newPos{
              updateSeekbar(seekBar.getPosition()++;}
          else {
              break; // we've reached out goal
          }
now = System.getCurrentTimeMillis(); //Edit - forgot to update the time!!
       }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM