Skip to content

Instantly share code, notes, and snippets.

@chrisbanes
Last active March 20, 2022 04:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbanes/4e613a76ebc6a2d3e64d3eee911e1e7d to your computer and use it in GitHub Desktop.
Save chrisbanes/4e613a76ebc6a2d3e64d3eee911e1e7d to your computer and use it in GitHub Desktop.
gesture_conflict_example
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.gestureexclusion
import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.os.Build
import android.util.AttributeSet
import android.widget.SeekBar
class MyAmazingSeekBar @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = android.R.attr.seekBarStyle
) : SeekBar(context, attrs, defStyle) {
private val gestureExclusionRects = mutableListOf<Rect>()
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
super.onLayout(changed, left, top, right, bottom)
if (changed) {
updateGestureExclusion()
}
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
updateGestureExclusion()
}
private fun updateGestureExclusion() {
// Skip this call if we're not running on Android 10+
if (Build.VERSION.SDK_INT < 29) return
// First, lets clear out any existing rectangles
gestureExclusionRects.clear()
// Now lets work out which areas should be excluded. For a SeekBar this will
// be the bounds of the thumb drawable.
thumb?.also { t ->
gestureExclusionRects += t.copyBounds()
}
// If we had other elements in this view near the edges, we could exclude them
// here too, by adding their bounds to the list
// Finally pass our updated list of rectangles to the system
systemGestureExclusionRects = gestureExclusionRects
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment