adding some click distance limitation to custom views

This commit is contained in:
tibbi 2019-01-16 22:14:36 +01:00
parent f495a0790e
commit 9abafd841f
6 changed files with 25 additions and 65 deletions

View file

@ -555,7 +555,7 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen
togglePlayPause() togglePlayPause()
} }
} else { } else {
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) { if (Math.abs(diffX) < CLICK_MAX_DISTANCE && Math.abs(diffY) < CLICK_MAX_DISTANCE && System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) {
toggleFullscreen() toggleFullscreen()
} }
} }
@ -673,7 +673,7 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen
togglePlayPause() togglePlayPause()
} }
} else { } else {
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) { if (Math.abs(diffX) < CLICK_MAX_DISTANCE && Math.abs(diffY) < CLICK_MAX_DISTANCE && System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) {
toggleFullscreen() toggleFullscreen()
} }
} }
@ -735,53 +735,6 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen
video_surface.invalidate() video_surface.invalidate()
} }
private fun handleEventt(event: MotionEvent) {
when (event.actionMasked) {
MotionEvent.ACTION_MOVE -> {
val diffX = event.x - mTouchDownX
val diffY = event.y - mTouchDownY
if (mIsDragged || (Math.abs(diffX) > mDragThreshold && Math.abs(diffX) > Math.abs(diffY))) {
if (!mIsDragged) {
arrayOf(video_curr_time, video_seekbar, video_duration).forEach {
it.animate().alpha(1f).start()
}
}
mIgnoreCloseDown = true
mIsDragged = true
var percent = ((diffX / mScreenWidth) * 100).toInt()
percent = Math.min(100, Math.max(-100, percent))
val skipLength = (mDuration * 1000f) * (percent / 100f)
var newProgress = mProgressAtDown + skipLength
newProgress = Math.max(Math.min(mExoPlayer!!.duration.toFloat(), newProgress), 0f)
val newSeconds = (newProgress / 1000).toInt()
setPosition(newSeconds)
resetPlayWhenReady()
}
}
MotionEvent.ACTION_UP -> {
mIgnoreCloseDown = false
if (mIsDragged) {
if (mIsFullscreen) {
arrayOf(video_curr_time, video_seekbar, video_duration).forEach {
it.animate().alpha(0f).start()
}
}
if (!mIsPlaying) {
togglePlayPause()
}
} else {
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) {
toggleFullscreen()
}
}
mIsDragged = false
}
}
}
// taken from https://github.com/Manuiq/ZoomableTextureView // taken from https://github.com/Manuiq/ZoomableTextureView
private inner class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener() { private inner class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScaleBegin(detector: ScaleGestureDetector): Boolean { override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {

View file

@ -731,15 +731,15 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S
val diffX = mTouchDownX - event.x val diffX = mTouchDownX - event.x
val diffY = mTouchDownY - event.y val diffY = mTouchDownY - event.y
if (Math.abs(diffX) < CLICK_MAX_DISTANCE && Math.abs(diffY) < CLICK_MAX_DISTANCE && System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) {
mTextureView.performClick()
} else {
val downGestureDuration = System.currentTimeMillis() - mTouchDownTime val downGestureDuration = System.currentTimeMillis() - mTouchDownTime
if (!mIgnoreCloseDown && Math.abs(diffY) > Math.abs(diffX) && diffY < -mCloseDownThreshold && downGestureDuration < MAX_CLOSE_DOWN_GESTURE_DURATION && mSaveScale == 1f) { if (!mIgnoreCloseDown && Math.abs(diffY) > Math.abs(diffX) && diffY < -mCloseDownThreshold && downGestureDuration < MAX_CLOSE_DOWN_GESTURE_DURATION && mSaveScale == 1f) {
activity?.supportFinishAfterTransition() activity?.supportFinishAfterTransition()
} }
mIgnoreCloseDown = false
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) {
mTextureView.performClick()
} }
mIgnoreCloseDown = false
} }
MotionEvent.ACTION_POINTER_DOWN -> { MotionEvent.ACTION_POINTER_DOWN -> {
mLastTouchX = event.x mLastTouchX = event.x

View file

@ -103,6 +103,7 @@ const val GO_TO_PREV_ITEM = "go_to_prev_item"
const val MAX_COLUMN_COUNT = 20 const val MAX_COLUMN_COUNT = 20
const val SHOW_TEMP_HIDDEN_DURATION = 300000L const val SHOW_TEMP_HIDDEN_DURATION = 300000L
const val CLICK_MAX_DURATION = 150 const val CLICK_MAX_DURATION = 150
const val CLICK_MAX_DISTANCE = 100
const val MAX_CLOSE_DOWN_GESTURE_DURATION = 400 const val MAX_CLOSE_DOWN_GESTURE_DURATION = 400
const val DRAG_THRESHOLD = 8 const val DRAG_THRESHOLD = 8
const val MONTH_MILLISECONDS = MONTH_SECONDS * 1000L const val MONTH_MILLISECONDS = MONTH_SECONDS * 1000L

View file

@ -5,6 +5,7 @@ import android.util.AttributeSet
import android.view.MotionEvent import android.view.MotionEvent
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.RelativeLayout import android.widget.RelativeLayout
import com.simplemobiletools.gallery.pro.helpers.CLICK_MAX_DISTANCE
import com.simplemobiletools.gallery.pro.helpers.CLICK_MAX_DURATION import com.simplemobiletools.gallery.pro.helpers.CLICK_MAX_DURATION
import com.simplemobiletools.gallery.pro.helpers.DRAG_THRESHOLD import com.simplemobiletools.gallery.pro.helpers.DRAG_THRESHOLD
@ -40,7 +41,9 @@ class InstantItemSwitch(context: Context, attrs: AttributeSet) : RelativeLayout(
mTouchDownTime = System.currentTimeMillis() mTouchDownTime = System.currentTimeMillis()
} }
MotionEvent.ACTION_UP -> { MotionEvent.ACTION_UP -> {
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) { val diffX = mTouchDownX - event.x
val diffY = mTouchDownY - event.y
if (Math.abs(diffX) < CLICK_MAX_DISTANCE && Math.abs(diffY) < CLICK_MAX_DISTANCE && System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) {
performClick() performClick()
} }
} }

View file

@ -13,6 +13,7 @@ import android.widget.TextView
import com.simplemobiletools.commons.extensions.onGlobalLayout import com.simplemobiletools.commons.extensions.onGlobalLayout
import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.R
import com.simplemobiletools.gallery.pro.extensions.audioManager import com.simplemobiletools.gallery.pro.extensions.audioManager
import com.simplemobiletools.gallery.pro.helpers.CLICK_MAX_DISTANCE
import com.simplemobiletools.gallery.pro.helpers.CLICK_MAX_DURATION import com.simplemobiletools.gallery.pro.helpers.CLICK_MAX_DURATION
import com.simplemobiletools.gallery.pro.helpers.DRAG_THRESHOLD import com.simplemobiletools.gallery.pro.helpers.DRAG_THRESHOLD
@ -106,7 +107,9 @@ class MediaSideScroll(context: Context, attrs: AttributeSet) : RelativeLayout(co
mLastTouchY = event.y mLastTouchY = event.y
} }
MotionEvent.ACTION_UP -> { MotionEvent.ACTION_UP -> {
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) { val diffX = mTouchDownX - event.x
val diffY = mTouchDownY - event.y
if (Math.abs(diffX) < CLICK_MAX_DISTANCE && Math.abs(diffY) < CLICK_MAX_DISTANCE && System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) {
callback(event.rawX, event.rawY) callback(event.rawX, event.rawY)
} }

View file

@ -89,15 +89,15 @@ class MyZoomableGifTextureView(context: Context, attrs: AttributeSet) : GifTextu
val diffX = mTouchDownX - event.x val diffX = mTouchDownX - event.x
val diffY = mTouchDownY - event.y val diffY = mTouchDownY - event.y
mCurrZoomMode = ZOOM_MODE_NONE mCurrZoomMode = ZOOM_MODE_NONE
if (System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) { if (Math.abs(diffX) < CLICK_MAX_DISTANCE && Math.abs(diffY) < CLICK_MAX_DISTANCE && System.currentTimeMillis() - mTouchDownTime < CLICK_MAX_DURATION) {
performClick() performClick()
} } else {
val downGestureDuration = System.currentTimeMillis() - mTouchDownTime val downGestureDuration = System.currentTimeMillis() - mTouchDownTime
val areDiffsOK = Math.abs(diffY) > Math.abs(diffX) && diffY < -mCloseDownThreshold val areDiffsOK = Math.abs(diffY) > Math.abs(diffX) && diffY < -mCloseDownThreshold
if (mSaveScale == 1f && !mIgnoreCloseDown && areDiffsOK && context.config.allowDownGesture && downGestureDuration < MAX_CLOSE_DOWN_GESTURE_DURATION) { if (mSaveScale == 1f && !mIgnoreCloseDown && areDiffsOK && context.config.allowDownGesture && downGestureDuration < MAX_CLOSE_DOWN_GESTURE_DURATION) {
mCloseDownCallback?.invoke() mCloseDownCallback?.invoke()
} }
}
mIgnoreCloseDown = false mIgnoreCloseDown = false
} }
MotionEvent.ACTION_POINTER_DOWN -> { MotionEvent.ACTION_POINTER_DOWN -> {