自定义seekbar仿开关,滑到最右边,自动回到左边

import static android.content.ContentValues.TAG;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SeekBarActivity extends Activity {

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custody);
    SeekBar seekbar = findViewById(R.id.skb);
    MySeekBarListener mySeekBarListener = new MySeekBarListener();
    seekbar.setOnSeekBarChangeListener(mySeekBarListener);

  }
  private class MySeekBarListener implements OnSeekBarChangeListener {
      private  int temp=0;
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      if (fromUser){
        Log.d(TAG, "onProgressChanged: "+progress);
        temp=progress;
      }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
      Log.d(TAG, "onStartTrackingTouch: 开始拖动");
    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
      Log.d(TAG, "onStopTrackingTouch: 停止拖动");
      if (temp>50){
        seekBar.setProgress(100);
      }else{
        seekBar.setProgress(0);
      }

    }

}

activity_custody.xml布局文件:



  
    seekbar_custody_button样式:

   

 
           seekbar的progressDrawable样式,可以修改第一进度条、第二进度条  res/drawable/seek)bar_custody.xml: 
  

  
  
    
      
        
        
        
      
    
  
seekbar的上面的拖动图标seek_bar_custody_thumb。xml




  





你可能感兴趣的:(自定义seekbar仿开关,滑到最右边,自动回到左边)