-
-
Save chrisbanes/4e613a76ebc6a2d3e64d3eee911e1e7d to your computer and use it in GitHub Desktop.
gesture_conflict_example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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