拖动时获取触摸位置 [英] Get touch position while dragging

查看:160
本文介绍了拖动时获取触摸位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢拖动一些视图。这些视图位于
a LinearLayout内,而其本身位于滚动视图中。

I have some views which I like to drag around. The views are within a LinearLayout, which itself is within a scrollview.

我想将当前手指(触摸)的位置移至
根据当前拖动的高度
来使滚动视图平滑滚动。

I want to get the position of the current finger (touch), to make a smooth scroll on my scrollview, depending of the height of the current drag.

在长按$ b $之后,我开始拖动b查看内置侦听器startDrag

I start my draggin after a long click with the View build-in listener startDrag

view.startDrag(null, shadowBuilder, view, 0);

我也能够在视图上获得拖动
的相对位置当前悬停在

I am also able to get the relativ position of the drag on the view that is currently hovered by

view.setOnDragListener(new OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            //get event positions
        }
    });

但这仅适用于当前拖动阴影位于
和DragEvent的视图提供相对位置,而不是原始位置。

But this only works for the view where the drag shadow is currently on and DragEvent only provides relative positions, not raw positions.

我需要的是手指的位置,而拖动发生。不幸的是,所有onTouchEvent都被消耗了

What I need is the position of the finger, while the drag happens. Unfortunately all onTouchEvents get consumed while dragging.

有人知道我该如何工作吗?

Does anybody know how I get this to work?

Ps:一种方法我当前使用的(某种)工作方式是通过dragEvent.relativePosition结合视图位置的
来计算触摸的
位置。但没有更好的方法吗?

推荐答案

好,因为似乎没有那么容易答案,我将为其他读者提供我自己的相对简单的解决方案,而无需进行手势检测。

OK, since there seems not to be an easier answer, I will provide my own relativly easy solution for further readers, with no need of gesture detection.

首先,您需要一个接收拖动事件的视图,然后,您便需要第二个视图
拖动事件ifself(或至少x和y坐标)。
通过获取视图位置和一些简单的添加操作,您可以获得
原始触摸位置。

At first you need the view that receives the drag event and at second the drag event ifself (or at least the x and y coordinates). With fetching of the view position and some simple addition you get the raw touch position.

此方法已通过 show测试指针位置开发人员选择
来提供正确的数据。

This method is tested with the show pointer position developer options to provide the correct data.

计算方法如下:

/**
 * @param item  the view that received the drag event
 * @param event the event from {@link android.view.View.OnDragListener#onDrag(View, DragEvent)}
 * @return the coordinates of the touch on x and y axis relative to the screen
 */
public static Point getTouchPositionFromDragEvent(View item, DragEvent event) {
    Rect rItem = new Rect();
    item.getGlobalVisibleRect(rItem);
    return new Point(rItem.left + Math.round(event.getX()), rItem.top + Math.round(event.getY()));
}

在onDragListener实现内部调用此方法:

Call this method inside of your onDragListener implementation:

@Override
public boolean onDrag(View v, DragEvent event) {
    switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            //etc etc. do some stuff with the drag event
            break;
        case DragEvent.ACTION_DRAG_LOCATION:
            Point touchPosition = getTouchPositionFromDragEvent(v, event);
            //do something with the position (a scroll i.e);
            break;
         default: 
   }
   return true;
}

附加:
如果您想确定触摸是否在内部在特定视图中,您可以
做这样的事情:

Addtional: If you want to determinate if the touch is inside of a specific view, you can do something like this:

 public static boolean isTouchInsideOfView(View view, Point touchPosition) {
    Rect rScroll = new Rect();
    view.getGlobalVisibleRect(rScroll);
    return isTouchInsideOfRect(touchPosition, rScroll);
}

public static boolean isTouchInsideOfRect(Point touchPosition, Rect rScroll) {
    return touchPosition.x > rScroll.left && touchPosition.x < rScroll.right //within x axis / width
            && touchPosition.y > rScroll.top && touchPosition.y < rScroll.bottom; //withing y axis / height
}

我还实现了基于此解决方案的listview。
这样,用户可以将项目拖出列表,并通过将项目拖到列表视图的顶部或底部来滚动列表。
如果有人对此感兴趣,我可以提供到
实现的进一步链接。

I have also implemented a smooth scroll on a listview based on this solution. SO that the user can drag an item out of a list, and scroll the list via dragging the item either to the top or bottom of the list view. If anybody is interested in it, I can provide a further link to the implementation.

干杯。

这篇关于拖动时获取触摸位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆