How to make slide to unlock button in android

前端 未结 11 559
心在旅途
心在旅途 2020-12-23 18:05

Hi I want a button that should work as \'slide to unlock\' button of IOS

in short I want a button that has no click effect but can slide left to right while drag and

11条回答
  •  温柔的废话
    2020-12-23 18:20

    You can rebuild the normal SeekBar to do what you want:

    With:

    • Starting point (20)
    • Crossing line (90)
    • And auto reset.

      seekBar.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { Integer point = 0; Integer startPoint = 0; boolean started = true;

          @Override
          public void onProgressChanged(SeekBar seekBar, int i, boolean wasUserInput) {
              point = i;
      
              if (started && i > 0 && wasUserInput) {
                  startPoint = new Integer(i);
                  started = false;
              }
          }
      
          @Override
          public void onStartTrackingTouch(SeekBar seekBar) {
          }
      
          @Override
          public void onStopTrackingTouch(SeekBar seekBar) {
              if (point > 90 && startPoint < 20) { // slided to the right correctly.
                  // TODO::
      
              } else { // reset.
                  resetSeekBar(seekBar, point);
              }
      
              startPoint = 0;
              started = true;
          }
      });
      

    And:

    /**
     * Resetting the seekbar, on point at a time.
     * @param seekBar                           Reference to the seekbar made smaller.
     * @param oldPoint                          The point where the dot is atm.
     */
    private void resetSeekBar(final SeekBar seekBar, final int oldPoint) {
        if (oldPoint > 0) {
            final int newPoint = oldPoint -1;
            seekBar.setProgress(newPoint);
    
            timer.schedule(new TimerTask() {
                final SeekBar seekBar = seekBarBid;
                @Override
                public void run() {
                    resetSeekBar(seekBar, newPoint);
                }
            }, 3);
    
        } else {
            seekBar.setProgress(oldPoint);
        }
    }
    

提交回复
热议问题