android:通过触摸移动移动视图(ACTION_MOVE)
我想做一个简单的控件:一个内部有视图的容器。如果我触摸容器并移动手指,我想移动视图以跟随我的手指。
我应该使用什么样的容器(布局)?如何做到这一点?
我不需要使用表面,而是使用简单的布局。
I'd like to do a simple control: a container with a view inside. If I touch the container and I move the finger, I want to move the view to follow my finger.
What kind of container (layout) should I use? How to do this?
I don't need to use a surface, but a simple layout.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我找到了一种使用 ViewPropertyAnimator 实现此目的的简单方法:
I've found an easy approach to do that with the ViewPropertyAnimator:
像这样的东西:
在
main.xml
中,只需RelativeLayout
和@+id/root
Something like this:
In
main.xml
justRelativeLayout
with@+id/root
触摸容器,视图将跟随您的手指。
xml 代码
Java 代码
Touch the container and the view will follow your finger.
xml code
Java code
按照@Andrew方法,如果您想将视图从其中心移动,您只需减去视图的一半高度和一半宽度即可。
Following the @Andrew approach, if you want to move the view from its center, you only have to substract the view's half height and half width to the movement.
创建一个自定义触摸监听器类(在 Kotlin 中):(
此代码限制您的视图从其父视图中拖出)
如何使用它?
parentView
是视图的父视图。Create a custom touch listener class (in Kotlin):
(This code restrict your view from dragging out of its parent view)
How to use it?
parentView
is the parent of your view.Kotlin 中的相同实现
Same implementation in Kotlin
我建议使用 view.translationX 和 view.translationY 来移动您的视图。
科特林片段:
I recommend to use view.translationX and view.translationY to move your views.
Kotlin snippet:
在下面的代码中,我创建了一个名为
RegionView
的东西 (git ),它是一个可重用容器,负责管理其每个嵌套子级的拖动和缩放操作。在这里,我们操纵子
View
的LayoutParams
的top
和left
系数来模拟图表的移动。通过将处理被理解为拖动操作的解释和被确定为缩放操作的解释解耦,我们可以提供对子View
的可靠操作。这里我展示了一个示例用例:
In the code below, I've created something called the
RegionView
(git), which is a reusable container responsible for managing drag and zoom operations for each of its nested children.Here, we manipulate the
top
andleft
coefficients of a childView
'sLayoutParams
to simulate movement about the diagram. By decoupling the interpretation of handling what's understood as a drag operation, and what is determined to be a scale operation, we can provide reliable manipulation of a childView
.Here I show an example use case:
在此示例中,您可以在其父边界内移动视图,无论其大小、完美的动画和捕获点击。
该解决方案优于其他评论的原因是,该方法使用方向板,它可以自行计算,并且不会中继视图位置,而视图位置是许多错误的根源。
这个版本从过去具有实时更新的网络实体的大东西中剥离出来,它应该可以工作。
你应该以这种方式使用它
,而不是
你也应该有这个,但这可以很容易地删除
如果你有 ScrollView 或双维 ScrollView 内的容器,你应该将此行添加到 onTouch
In this example you can move the view within it's parent bounds no matter it's size, flawless animation, and catch clicks.
The reason that this solution is superior to other comments is that this approach uses a Directional Pad which calculate itself and won't relay on the View positions which is a the source for a-lot of bugs.
This version got stripped from the big stuff which used to have network entity that gets updated live and such, it should work.
you should use it this way
and than
you should have this too but this can be easily removed
If you have the container inside a ScrollView or double dimension ScrollView you should add this line to the onTouch
与@Alex Karshin的答案相同,我改变了一点。
The same as @Alex Karshin's answer, I change a bit.
对 @Vyacheslav Shylkin 提供的解决方案进行了一些更改,以消除手动输入数字的依赖性。
Changed a bit a solution provided by @Vyacheslav Shylkin to remove dependencies of manually entered numbers.
//如果您想移动相机或其他任何东西,请按照我的方法进行操作
//我在相机上实现的情况你可以将它应用到你想要的任何地方
//if you want to move your camera or whatever then do it by following method..in my
//case I am implementing on camera you can apply it on whateveer u want
kotlin 的简单代码:
然后像这样调用它:
Simple code for kotlin:
And then call it like this: