有一个这样的场景,一个音乐条 包裹 ,音乐条里面有一个头像,点击头像可以展开收缩条,但是 触摸头像的时候还允许滑动,那么写了几次代码解决了问题,但是没法解决一个按下去然后拖动 刚开始会跳动的问题
写法就是自定义viewgroup重写onInterceptTouchEvent
方法
判断点击距离,如果判断距离没超过3dp就返回true,下发给view,否则返回true,
child不需要写任何处理代码
public boolean onInterceptTouchEvent(MotionEvent event) {//好像只在down的时候触发
final float scale = getContext().getResources().getDisplayMetrics().density;//density=dpi/160
int clickDistance = (int) (3 * scale + 0.5f);
int actionMasked = event.getActionMasked();
switch (actionMasked) {
case MotionEvent.ACTION_DOWN:
hasMove = false;
downXDispatch = event.getX();
downYDispatch = event.getY();
/*
downXDispatch = event.getRawX();
downYDispatch = event.getRawY();
*/
Prt.w(TAG, "onInterceptTouchEvent down->" + downYDispatch + "," + event.getY());
return false;//首先子类优先处理 点击事件,
case MotionEvent.ACTION_MOVE:
float distanceX = event.getX() - downXDispatch;//表示触摸到哪里,但是 你滑动具体是多少是得转换的
float distanceY = event.getY() - downYDispatch;
if (hasMove == false && Math.abs(distanceX) <= clickDistance && Math.abs(distanceY) <= clickDistance) {
Prt.w(TAG, "onInterceptTouchEvent 点击事件,交给子类处理触摸距离x:" + distanceX + ",distancey:" + distanceY + ",点击距离:" + clickDistance + "," + ",downY:" + downYDispatch + ",downX:" + downXDispatch);
return false;
} else {
Prt.w(TAG, "onInterceptTouchEvent 触摸事件,交给自己处理");
return true;//交给自己处理
}
case MotionEvent.ACTION_UP:
if (hasMove) {
return false;
}
break;
}
return super.onInterceptTouchEvent(event);
}
private float downXDispatch;
private float downYDispatch;
float downX = 0;
float downY = 0;
// public boolean onTouch(View v, MotionEvent event) {
public boolean onTouchEvent(MotionEvent event) {
int actionMasked = event.getActionMasked();
switch (actionMasked) {
// switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// pressTime = System.currentTimeMillis();
downX = event.getRawX();
downY = event.getRawY();
Prt.w(TAG, "onTouchEvent ACTION_DOWN");
return true;
case MotionEvent.ACTION_MOVE:
float distanceX = event.getRawX() - downX;//表示触摸到哪里,但是 你滑动具体是多少是得转换的
float distanceY = event.getRawY() - downY;
downX = event.getRawX();
downY = event.getRawY();
float translationX = this.getTranslationX() + distanceX;
float translationY = this.getTranslationY() + distanceY;
if (onMoveListener != null) {
onMoveListener.onTransMove(translationX, translationY);
}
Prt.w(TAG, "onTouchEvent ACTION_MOVE,will settransX:" + translationX + ",will transY:" + translationY + ",beforeTranslationX:" + this.getTranslationX() + ",transY:" + this.getTranslationY() + ",distnceX:" + distanceX + ",distanceY:" + distanceY);
this.setTranslationX(translationX);
this.setTranslationY(translationY);
/* offsetLeftAndRight((int) translationX);
offsetTopAndBottom((int) translationY);*/
return true;
case MotionEvent.ACTION_UP:
Prt.w(TAG, "onTouchEvent ACTION_UP");
default:
return false;
}
}
// });
// }
interface OnMoveListener {
void onTransMove(float x, float y);
}
public void setOnMoveListener(OnMoveListener onMoveListener) {
this.onMoveListener = onMoveListener;
}
OnMoveListener onMoveListener;
o.tv W/WidthAnimLinearView: onTouchEvent ACTION_UP
03-28 18:02:22.438 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent down->81.0,81.0
03-28 18:02:22.459 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent 点击事件,交给子类处理触摸距离x:0.0,distancey:0.0,点击距离:9,,downY:81.0,downX:65.0
03-28 18:02:22.476 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent 点击事件,交给子类处理触摸距离x:0.58836365,distancey:0.0,点击距离:9,,downY:81.0,downX:65.0
03-28 18:02:22.492 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent 点击事件,交给子类处理触摸距离x:3.1565247,distancey:0.5782471,点击距离:9,,downY:81.0,downX:65.0
03-28 18:02:22.509 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent 点击事件,交给子类处理触摸距离x:8.330505,distancey:2.5826416,点击距离:9,,downY:81.0,downX:65.0
03-28 18:02:22.525 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onInterceptTouchEvent 触摸事件,交给自己处理
03-28 18:02:22.542 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:-99.310486,will transY:21.172363,beforeTranslationX:0.0,transY:0.0,distnceX:-99.310486,distanceY:21.172363
03-28 18:02:22.558 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:16.042633,will transY:2.4191895,beforeTranslationX:0.0,transY:0.0,distnceX:16.042633,distanceY:2.4191895
03-28 18:02:22.575 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:12.2222595,will transY:0.40844727,beforeTranslationX:0.0,transY:0.0,distnceX:12.2222595,distanceY:0.40844727
03-28 18:02:22.591 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:11.5729065,will transY:1.0,beforeTranslationX:0.0,transY:0.0,distnceX:11.5729065,distanceY:1.0
03-28 18:02:22.608 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:11.417755,will transY:0.58898926,beforeTranslationX:0.0,transY:0.0,distnceX:11.417755,distanceY:0.58898926
03-28 18:02:22.624 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:8.368561,will transY:0.41101074,beforeTranslationX:0.0,transY:0.0,distnceX:8.368561,distanceY:0.41101074
03-28 18:02:22.641 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:9.164856,will transY:1.5797119,beforeTranslationX:0.0,transY:0.0,distnceX:9.164856,distanceY:1.5797119
03-28 18:02:22.657 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:9.862762,will transY:0.4202881,beforeTranslationX:0.0,transY:0.0,distnceX:9.862762,distanceY:0.4202881
03-28 18:02:22.674 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:10.573273,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:10.573273,distanceY:0.0
03-28 18:02:22.690 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:8.449524,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:8.449524,distanceY:0.0
03-28 18:02:22.707 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:8.404022,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:8.404022,distanceY:0.0
03-28 18:02:22.723 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:5.392334,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:5.392334,distanceY:0.0
03-28 18:02:22.740 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:5.5820007,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:5.5820007,distanceY:0.0
03-28 18:02:22.756 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:5.986145,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:5.986145,distanceY:0.0
03-28 18:02:22.773 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:6.9989014,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:6.9989014,distanceY:0.0
03-28 18:02:22.789 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:7.58667,will transY:-1.0,beforeTranslationX:0.0,transY:0.0,distnceX:7.58667,distanceY:-1.0
03-28 18:02:22.806 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:5.990906,will transY:-1.0,beforeTranslationX:0.0,transY:0.0,distnceX:5.990906,distanceY:-1.0
03-28 18:02:22.822 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:9.007904,will transY:-1.0,beforeTranslationX:0.0,transY:0.0,distnceX:9.007904,distanceY:-1.0
03-28 18:02:22.839 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:7.448944,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:7.448944,distanceY:0.0
03-28 18:02:22.855 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:6.0026855,will transY:-1.0,beforeTranslationX:0.0,transY:0.0,distnceX:6.0026855,distanceY:-1.0
03-28 18:02:22.872 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:6.001953,will transY:0.0,beforeTranslationX:0.0,transY:0.0,distnceX:6.001953,distanceY:0.0
03-28 18:02:22.888 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:5.967499,will transY:-0.5780029,beforeTranslationX:0.0,transY:0.0,distnceX:5.967499,distanceY:-0.5780029
03-28 18:02:22.905 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:8.717407,will transY:-0.9971924,beforeTranslationX:0.0,transY:0.0,distnceX:8.717407,distanceY:-0.9971924
03-28 18:02:22.921 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:10.924286,will transY:-1.4248047,beforeTranslationX:0.0,transY:0.0,distnceX:10.924286,distanceY:-1.4248047
03-28 18:02:22.936 8501-8501/cn.qssq666.demo W/WidthAnimLinearView: onTouchEvent ACTION_MOVE,will settransX:11.624298,will transY:-3.0,beforeTranslationX:0.0,transY:0.0,distnceX:11.624298,distanceY:-3.0
从上面的第一个onTouchEvent
可以看出来会有一个bug,刚按下去的时候导致view发生超大的移动
这个改成获取屏幕的坐标就ok.