Touch Event 로 슬라이드 시 SeekBar도 변경
public class SlideSeekBar extends SeekBar{
/** Factor */
private float factor = 1;
public float getFactor(){return factor;}
public void setFactor(float factor){this.factor = factor;}
/** Constructors */
public SlideSeekBar(Context context) {
super(context);
this.customiseOnTouch();
}
public SlideSeekBar(Context context, AttributeSet attrs){
super(context, attrs);
this.customiseOnTouch();
}
public SlideSeekBar(Context context, AttributeSet attrs, int defStyle){
super(context, attrs);
this.customiseOnTouch();
}
/** Touch gesture */
private OnSeekBarChangeListener onSlideSeekBarChangeListener;
public void setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener onSeekBarChangeListener){
this.onSlideSeekBarChangeListener = onSeekBarChangeListener;
super.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (!b)
onSlideSeekBarChangeListener.onProgressChanged(seekBar, i, b);
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});
};
public void customiseOnTouch(){
this.tapGestureListener = new GestureDetector(this.getContext(), new TapGestureListener());
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Start tracking
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
onSlideSeekBarChangeListener.onStartTrackingTouch(SlideSeekBar.this);}
// Stop tracking
if (motionEvent.getAction() == MotionEvent.ACTION_UP)
onSlideSeekBarChangeListener.onStopTrackingTouch(SlideSeekBar.this);
// Progress change
tapGestureListener.onTouchEvent(motionEvent);
return true;
}
});
}
GestureDetector tapGestureListener;
private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent e){
float percentageMoved = e.getX()/(float)(SlideSeekBar.this.getRight()-SlideSeekBar.this.getLeft());
int newProgress = (int)(percentageMoved*SlideSeekBar.this.getMax());
if (newProgress == SlideSeekBar.this.getProgress())
return true;
SlideSeekBar.this.setProgress(newProgress);
onSlideSeekBarChangeListener.onProgressChanged(SlideSeekBar.this, getProgress(), true);
return true;
}
public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){
float percentageMoved = -distanceX/(float)(SlideSeekBar.this.getRight()-SlideSeekBar.this.getLeft());
int newProgress = SlideSeekBar.this.getProgress() + (int)(percentageMoved*SlideSeekBar.this.getMax()*factor);
if (newProgress == SlideSeekBar.this.getProgress())
return true;
SlideSeekBar.this.setProgress(newProgress);
onSlideSeekBarChangeListener.onProgressChanged(SlideSeekBar.this, getProgress(), true);
return true;
}
}
}
'Study > Android' 카테고리의 다른 글
[Android] wifi 원격 디버깅 (0) | 2019.06.14 |
---|---|
[Android] GestureDetector Top/Bottom/Up/Down (0) | 2019.06.14 |
[Android] 가로 리스트 뷰 (0) | 2019.06.14 |
[Android] google sign in 연동하기 (0) | 2019.06.14 |
[Android] 현재 기기 저장소 용량 확인 (0) | 2019.06.14 |