搜索栏缩略图 onclicklistener
seekbar thumb onclicklistener
我需要自定义默认 android 搜索栏来控制音乐播放器。我知道这听起来很简单,但我就是不知道如何设置 seekbar thumb 侦听器。我想控制音乐并相应地更改图标以在用户按下搜索栏拇指图标时播放和暂停。我怎样才能做到这一点?我知道这是可能的,因为我之前看到过像 PocketGuide 这样的应用程序实现了这个功能。这是 PocketGuide 应用程序的屏幕截图
也许这对你有帮助。根据您的需要调整代码。
public class SeekbarWithThumbTouch extends SeekBar {
private int scaledTouchSlop = 0;
private float initTouchX = 0;
private boolean thumbPressed = false;
public SeekbarWithThumbTouch(Context context) {
super(context);
init(context);
}
public SeekbarWithThumbTouch(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SeekbarWithThumbTouch(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Drawable thumb = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
thumb = getThumb();//works only for API >=16!
if (thumb != null) {
//contains current position of thumb in view as bounds
RectF bounds = new RectF(thumb.getBounds());
thumbPressed = bounds.contains(event.getX(), event.getY());
if (thumbPressed) {
Log.d("Thumb", "pressed");
initTouchX = event.getX();
return true;
}
}
break;
case MotionEvent.ACTION_UP:
if (thumbPressed) {
Log.d("Thumb", "was pressed -- listener call");
thumbPressed = false;
}
break;
case MotionEvent.ACTION_MOVE:
if (thumbPressed) {
if (Math.abs(initTouchX - event.getX()) > scaledTouchSlop) {
initTouchX = 0;
thumbPressed = false;
return super.onTouchEvent(event);
}
Log.d("Thumb", "move blocked");
return true;
}
break;
}
return super.onTouchEvent(event);
}
}
我需要自定义默认 android 搜索栏来控制音乐播放器。我知道这听起来很简单,但我就是不知道如何设置 seekbar thumb 侦听器。我想控制音乐并相应地更改图标以在用户按下搜索栏拇指图标时播放和暂停。我怎样才能做到这一点?我知道这是可能的,因为我之前看到过像 PocketGuide 这样的应用程序实现了这个功能。这是 PocketGuide 应用程序的屏幕截图
也许这对你有帮助。根据您的需要调整代码。
public class SeekbarWithThumbTouch extends SeekBar {
private int scaledTouchSlop = 0;
private float initTouchX = 0;
private boolean thumbPressed = false;
public SeekbarWithThumbTouch(Context context) {
super(context);
init(context);
}
public SeekbarWithThumbTouch(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SeekbarWithThumbTouch(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Drawable thumb = null;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
thumb = getThumb();//works only for API >=16!
if (thumb != null) {
//contains current position of thumb in view as bounds
RectF bounds = new RectF(thumb.getBounds());
thumbPressed = bounds.contains(event.getX(), event.getY());
if (thumbPressed) {
Log.d("Thumb", "pressed");
initTouchX = event.getX();
return true;
}
}
break;
case MotionEvent.ACTION_UP:
if (thumbPressed) {
Log.d("Thumb", "was pressed -- listener call");
thumbPressed = false;
}
break;
case MotionEvent.ACTION_MOVE:
if (thumbPressed) {
if (Math.abs(initTouchX - event.getX()) > scaledTouchSlop) {
initTouchX = 0;
thumbPressed = false;
return super.onTouchEvent(event);
}
Log.d("Thumb", "move blocked");
return true;
}
break;
}
return super.onTouchEvent(event);
}
}