如何动画seekbar thumb移动到特定位置

时间:2015-02-04 18:36:44

标签: android

我定义了不同的锁定位置:

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

onStopTrackingTouch我计算哪个锁定位置最接近我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];
}

我现在需要以平滑的动画以某种方式将SeekBar的进度状态设置为我的锁定位置。

我怎么能阻止那个动画?

编辑

感谢@bwoogie在这里使用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();
}

1 个答案:

答案 0 :(得分:0)

我要做的是创建一个循环(在新线程中),如果已经过了足够的时间,则每隔几毫秒检查一次,如果是,那么我们可以更新seekBar。这是基本的想法,只需将它放入一个线程并实现updateSeekBar()方法,因为它在一个线程中你无法直接访问UI线程。

    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!!
       }
    }