自定义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布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <SeekBar
    android:id="@+id/skb"
    style="@style/seekbar_custody_button"
    android:maxHeight="60dp"
    android:minHeight="60dp"
    android:progress="0"/>
</android.support.constraint.ConstraintLayout>
    seekbar_custody_button样式:

    <?xml version="1.0" encoding="utf-8"?>

 
 
 
 <style name="seekbar_custody_button">
<item name="android:layout_width">363dp</item>
<item name="android:layout_height">wrap_content</item> <item name="android:max">100</item> <item name="android:progressDrawable">@drawable/seek_bar_custody</item> <item name="android:thumb">@drawable/seek_bar_custody_thumb</item> <item name="android:layout_marginLeft">26dp</item> <item name="android:layout_marginRight">26dp</item> <item name="android:layout_marginTop">34dp</item></style>          seekbar的progressDrawable样式,可以修改第一进度条、第二进度条  res/drawable/seek)bar_custody.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@android:id/background"
    android:drawable="@drawable/bg_slide"/>
  <item android:id="@android:id/progress">
    <scale android:scaleWidth="100%">
      <shape>
        <corners android:radius="8dp"/>
        <!--<solid android:color="#51A0FE"/>-->
        <gradient
          android:startColor="#9EAFC8"
          android:endColor="#9EAFC8"/>
      </shape>
    </scale>
  </item>
</layer-list>
 
 
seekbar的上面的拖动图标seek_bar_custody_thumb。xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <SeekBar
    android:id="@+id/skb"
    style="@style/seekbar_custody_button"
    android:maxHeight="60dp"
    android:minHeight="60dp"
    android:progress="0"/>
</android.support.constraint.ConstraintLayout>





猜你喜欢

转载自blog.csdn.net/sinat_36345777/article/details/80747216