问题标签 [motionevent]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
android - Button.onTouch() 没有被调用
我有一个按 ID 找到的按钮 btnSpeak,然后我尝试设置 MotionEventListener 包 com.ctc.android.widget;
这是在 Activity 的 onCreate 方法中完成的,但是当 Activity 运行并触摸按钮时,该事件永远不会触发,我看不出哪里出错了。不知道是不是只见树木不见木材。
java - 检测安卓屏幕上的触摸位置是如何工作的?
我们可以使用以下函数获取触摸位置在屏幕上的坐标:
getX()
和getY()
函数究竟返回了什么?它是否返回像素值?当手指触摸屏幕位置时,它可能会触摸多个像素。那么这个函数究竟返回了什么?
再次假设,在我用手指触摸的区域,屏幕上有多个像素点,如下图:
假设我需要点击并拖动像素 1 处的点,但我的手指触摸的区域覆盖了像素 1 和 2。如何准确检测单击了哪个像素,以便在我点击并拖动像素 1 或 2 时需要它?
java - 检查 MotionEvent.ACTION_UP 是否超出图像视图
用户将触摸图像,重要的是他是否将手指放在该图像中
我尝试编写 a onTouchListner()
,然后使用,swich case
但我不知道如何继续
提前致谢
android - 获取 MotionEvent Android 的中心点
我需要计算 MotionEvent 的中心,例如当用户用手指触摸屏幕时,我想知道 x,y 中的点是这个手指触摸的中心,我目前使用 motionEvent.getX()和motionEvent.getY(),但它似乎让我在手指触摸的最底部得到了一些点,这对我来说很糟糕......那么它是如何完成的?
android - 在 onTouch 期间未触发 MotionEvent.ACTION_CANCEL
我在ACTION_CANCEL
没有触发的地方遇到了这个问题,我已经在我的另一个项目中实现了它并且它工作正常。似乎这ACTION_UP
是唯一MotionEvent
被调用的ACTION_DOWN
。ACTION_CANCEL
一旦我的手指不再在视图中或屏幕外,我想触发。
示例场景:我单击一个 LinearLayout 的视图 btw,ACTION_DOWN
其背景更改为图像的“单击/变暗”版本,并且当ACTION_UP
触发时,仅当手指在 LinearLayout 内时,其背景才更改回默认图像. 现在的问题是,当我按下它并将手指放在屏幕上并将手指拖到 LinearLayout 之外时,ACTION_UP
仍然会在不应该触发的地方触发。
这是我的代码:
其中:dimView 是一个 LinearLayout
android - 如何仅在该特定相对布局中移动edittext或imageview?
我已经为移动edittext和imageview做了这段代码。它移动得很好,但它也在相对布局之外移动。我必须限制仅在特定布局中移动。有人有建议吗?
android - Drawing a line following finger - motionevent.getX() and getY() incorrect UPDATE
UPDATE: please read entire question again :-)
Background:
I have created a grid of dots by creating a custom view and then adding these views to a TableLayout
. The aim is for me to be able to draw a line from one of these dots to another dot in a way so that when a dot is pressed, a line is started from the center of the pressed dot to the point where the finger is currently touching. When the finger is dragged over another dot, the line then finishes at the center of that dot. The result being a line drawn from the center of the pressed dot to the center of the dot which was dragged over by the finger.
To create a line going over the TableLayout
, I created a new custom view which just created a line between points with the canvas.drawLine()
method. I then created a FrameLayout
in which I put the TableLayout and the LineView. This meant the line view would be able to be drawn on top of the TableLayout.
Current Situation:
I have added touch listener in the DotView class so that it can have its own “touch feedback”. There is also another touch listener in the activity which I am trying to use to get the points needed to draw the line. The touch listeners are working fine as far as I can tell - they are not interfering with each other.
The problem is getting the correct coordinates to plot the line. I have tried a number of ways to get the coordinates of the center of the dot that was pressed (the starting point of the line), but I still have not managed it. This question, has more information: Get the coordinates of the center of a view . The second part is getting the correct end point of the line. For the purposes of this question, I would just like the end of the line to follow the position of the finger. I thought it was as easy as motionevent.getX() / getY()
but this hasn’t worked. What happened instead was that there was a mix up between the coordinates on a scale relative to the layout of the dot and the coordinates relative to the whole layout/screen.
Simply put: the getX() and getY() values are incorrect, and this is what I am trying to solve here.
As shown in the screenshots, when I press down on a dot, the start point of the line is roughly in the right place, but the end point is way off. I can’t really explain why.
I have tried getRawX()
and getRawY()
and they return more much more accurate values, but they are still incorrect by the amount of padding (or something like that - I don’t 100% understand).
This shows my code
In my Activity
:
And finally the LineView
:
NOTES:
I am calling the top left hand dot
"dot 1"
.The screenshots may not be entirely accurate as my finger moves slightly when I take the screenshot, but the behaviour I described is happening.
If any other information/code is wanted, I will happily provide it.
Thanks for taking the time to read this - sorry it is so long!
android - 当我触摸按钮时,如何获取触摸相对于整个视图的 x/y 位置?
我想要相对于整个屏幕的触摸信息,但我得到的只是相对于按钮的触摸信息。
我从事件中得到的 x 和 y 与按钮有关(0, 0 是按钮的角,而不是屏幕的角)。仅当按下按钮时,如何获取相对于整个屏幕的 x 和 y 位置?
android - 拖动 EditText 字段,同时允许其可编辑。
我可以使用以下代码在视图中上下拖动 EditText 字段。毫不奇怪,我不能再编辑 EditText 字段。我需要这样做,以便当我点击该字段时,键盘会出现,并且当我拖动它时,拖动代码就会被实现。我怎样才能区分这两者。我想我需要以某种方式实现 GestureDetector 。关于如何做到这一点的任何建议?
android - Android检测双指长按
我正在创建一个可以在图片上绘制的应用程序。绘图是通过一个pictureView、一个带有位图的canvas和一个onTouchListener来实现的。我想添加一个功能,当有两根手指短按时将撤消最后一个操作。我通过这样做实现了它:
当有两根手指长按时,我想撤消所有绘图动作。我尝试过这样的事情,但它只会检测一次。
我怎样才能达到我想要的?