fixing some multitouch gesture related glitches

This commit is contained in:
tibbi
2022-02-23 22:15:44 +01:00
parent 4223488b65
commit 18555d4a5e

View File

@@ -56,6 +56,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
private var mAllowMovingZooming = true private var mAllowMovingZooming = true
private var mIsEraserOn = false private var mIsEraserOn = false
private var mWasMultitouch = false private var mWasMultitouch = false
private var mIgnoreTouches = false
private var mWasScalingInGesture = false
private var mBackgroundColor = 0 private var mBackgroundColor = 0
private var mCenter: PointF? = null private var mCenter: PointF? = null
@@ -103,6 +105,16 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
} }
val action = event.actionMasked val action = event.actionMasked
if (mIgnoreTouches && action == MotionEvent.ACTION_UP) {
mIgnoreTouches = false
mWasScalingInGesture = false
return true
}
if (mIgnoreTouches) {
return true
}
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) { if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
mActivePointerId = event.getPointerId(0) mActivePointerId = event.getPointerId(0)
} }
@@ -130,6 +142,7 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
when (action) { when (action) {
MotionEvent.ACTION_DOWN -> { MotionEvent.ACTION_DOWN -> {
mWasScalingInGesture = false
mWasMultitouch = false mWasMultitouch = false
mStartX = x mStartX = x
mStartY = y mStartY = y
@@ -160,25 +173,19 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
} }
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
mActivePointerId = INVALID_POINTER_ID mActivePointerId = INVALID_POINTER_ID
actionUp() actionUp(false)
mWasScalingInGesture = false
} }
MotionEvent.ACTION_POINTER_DOWN -> { MotionEvent.ACTION_POINTER_DOWN -> {
mWasMultitouch = true mWasMultitouch = true
mTouchSloppedBeforeMultitouch = mLastMotionEvent.isTouchSlop(pointerIndex, mStartX, mStartY) mTouchSloppedBeforeMultitouch = mLastMotionEvent.isTouchSlop(pointerIndex, mStartX, mStartY)
} }
MotionEvent.ACTION_POINTER_UP -> { MotionEvent.ACTION_POINTER_UP -> {
val upPointerIndex = event.actionIndex mIgnoreTouches = true
val pointerId = event.getPointerId(upPointerIndex) actionUp(!mWasScalingInGesture)
if (pointerId == mActivePointerId) {
val newPointerIndex = if (upPointerIndex == 0) 1 else 0
mLastTouchX = event.getX(newPointerIndex)
mLastTouchY = event.getY(newPointerIndex)
mActivePointerId = event.getPointerId(newPointerIndex)
}
} }
} }
mLastMotionEvent = MotionEvent.obtain(event) mLastMotionEvent = MotionEvent.obtain(event)
invalidate() invalidate()
@@ -349,8 +356,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
mCurY = y mCurY = y
} }
private fun actionUp() { private fun actionUp(forceLineDraw: Boolean) {
if (!mWasMultitouch) { if (!mWasMultitouch || forceLineDraw) {
mPath.lineTo(mCurX, mCurY) mPath.lineTo(mCurX, mCurY)
// draw a dot on click // draw a dot on click
@@ -390,6 +397,12 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
private inner class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener() { private inner class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScale(detector: ScaleGestureDetector): Boolean { override fun onScale(detector: ScaleGestureDetector): Boolean {
if (!mWasScalingInGesture) {
mPath.reset()
}
mIgnoreTouches = true
mWasScalingInGesture = true
mScaleFactor *= detector.scaleFactor mScaleFactor *= detector.scaleFactor
mScaleFactor = max(0.1f, min(mScaleFactor, 10.0f)) mScaleFactor = max(0.1f, min(mScaleFactor, 10.0f))
setBrushSize(mCurrBrushSize) setBrushSize(mCurrBrushSize)