1.在xml布局文件中添加SeekBar:
2.在Activity中设置SeekBar.OnSeekBarChangeListener监听器,实现透明圆点的效果:
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
private SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//在SeekBar上添加透明圆点
LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
Drawable circleDrawable = getResources().getDrawable(R.drawable.circle);
layerDrawable.setDrawableByLayerId(android.R.id.secondaryProgress, circleDrawable);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//当开始拖动SeekBar时,将透明圆点设为不可见
LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
Drawable circleDrawable = layerDrawable.findDrawableByLayerId(android.R.id.secondaryProgress);
circleDrawable.setAlpha(0);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//当停止拖动SeekBar时,将透明圆点设为可见
LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
Drawable circleDrawable = layerDrawable.findDrawableByLayerId(android.R.id.secondaryProgress);
circleDrawable.setAlpha(255);
}
}
3.添加透明圆点的drawable资源文件,circle.xml:
这样,在拖动SeekBar时就会出现一个透明圆点跟随滑动。