当前位置: 代码迷 >> 综合 >> Android onScale onScaleBegin不执行原因
  详细解决方案

Android onScale onScaleBegin不执行原因

热度:21   发布时间:2023-10-19 06:19:20.0

既然已经看到这个方法了,说明再用到

ScaleGestureDetector这个缩放类遇到问题了?

说到这里先看看双指缩放问题 一般onTouchEvent进行事件处理 

MotionEvent.ACTION_DOWN 按下的时候 
MotionEvent.ACTION_POINTER_DOWN 多指触发
MotionEvent.ACTION_MOVE 事件执行者,会触发多次
 MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP 手指抬起触发

一般正常情况下,多指是不会触发MotionEvent.ACTION_POINTER_DOWN这个方法的,需要加入

MotionEvent.ACTION_MASK这个方法才会触发,意思是再这个界定的范围内才会执行,这个是之前遇到的坑

再回到上面的问题,一般原来的方法是:

@Override   public boolean onTouchEvent(MotionEvent event) {  int pointCount = event.getPointerCount();  if(pointCount==1) return myDetector.onTouchEvent(event);   else  return mScaleGestureDetector.onTouchEvent(event); }  

但会遇到一个问题pointCount 永远等于1,需要和上面的方法连着用,但要特别注意的一点是,不要再MotionEvent.ACTION_POINTER_DOWN执行 return mScaleGestureDetector.onTouchEvent(event),因为不会执行onscale方法,原因:

ScaleGestureDetector.onTouchEvent方法 源码很多看主要的部分
// Handle motion; focal point and span/scale factor are changing.if (action == MotionEvent.ACTION_MOVE) {mCurrSpanX = spanX;mCurrSpanY = spanY;mCurrSpan = span;boolean updatePrev = true;if (mInProgress) {updatePrev = mListener.onScale(this);}if (updatePrev) {mPrevSpanX = mCurrSpanX;mPrevSpanY = mCurrSpanY;mPrevSpan = mCurrSpan;mPrevTime = mCurrTime;}}

要执行onscale方法,必须是

action == MotionEvent.ACTION_MOVE

才会执行。最终我写的:

 event?.action?.let {when (it and MotionEvent.ACTION_MASK) {MotionEvent.ACTION_DOWN -> {mode = DRAG}MotionEvent.ACTION_POINTER_DOWN -> {mode = ZOOMval pointCount = event.pointerCountLog.e("mlt",".....pointCount...${pointCount}.......")}MotionEvent.ACTION_MOVE -> {if (mode == ZOOM) {val pointCount = event.pointerCountLog.e("mlt",".....pointCount...${pointCount}.......")return if (pointCount == 1) {myDetector?.onTouchEvent(event) == true} else {mScaleGestureDetector?.onTouchEvent(event) == true}}}}return true}return super.onTouchEvent(event)

ok了

Android GestureDetector手势识别与多点触控探究

  相关解决方案