Android 沿路径拖动视图

时间:2021-03-12 09:42:32

标签: android kotlin

所以我有路径,我想在该路径中垂直拖动我的视图。 这是我的 onTouchListener 视图代码:

val point = FloatArray(2)
val pathMeasure = PathMeasure(path, true)

 tooltipView.setOnTouchListener(object :View.OnTouchListener{
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                onDataPointTouchListener()
                if (event?.action == MotionEvent.ACTION_MOVE){
                    v?.performClick()
                    pathMeasure.getPosTan(event.x, point, null) // This is where I get path point
                    v?.x = point[0] - tooltipWith / 2
                    v?.y = point[1] - tooltipHeight
                }else if (event?.action == MotionEvent.ACTION_UP){
                    tooltipView.animate().translationX(previousX - tooltipWith / 2)
                    tooltipView.animate().translationY(previousY - tooltipHeight)
                    layoutDate.visible()
                }
                return true
            }
        })

谁能帮帮我。我的路径是一条曲线。我希望能够在该行中拖动视图。就像一个搜索栏:

enter image description here

2 个答案:

答案 0 :(得分:1)

您尝试做的事情有些困难,取决于您愿意采取哪些捷径。

我能想到的最简单的是: 使用 pathMeasure 走路径并决定您的手指最接近路径上的哪个点(与 event.x/y 相比,在整个路径上循环)。将位置移动到该点。这不像seekbar那样流畅。哪个动画到你的手指。

答案 1 :(得分:0)

使用 MotionLayout(约束布局 2 库)要简单得多——您只需要设置一个包含 2 个约束集的 XML 文件,其余的由库处理。

您还可以在特定进度添加路径坐标,库会创建路径插值。

参考文献:

  1. Manage motion and widget animation with MotionLayout
  2. MotionLayout examples