android - 如何查找 ACTION_MOVE 触摸事件是否在圆形路径上 : Android

标签 android math touch

我正在通过触摸事件围绕圆圈移动图像。 我希望用户触摸图像,当用户围绕圆圈拖动该图像时,它会移动,否则不会移动。

有人可以帮忙计算一下如何检查手指是否沿着圆圈移动并相应地移动图像。

谢谢。

更新:

我正在尝试围绕一个圆圈旋转图像。它已经放在圆边上。

但在触摸和移动 Action 时,它以自身为中心,然后开始围绕定义的半径移动。

谁能看到代码并让我知道哪里出错了。

谢谢。

@Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:


                        mInitialX = event.getX();
                        mInitialY = event.getY();

                    break;

                case MotionEvent.ACTION_MOVE:

                    mEndX = event.getX();
                    mEndY = event.getY();

                    float deltaX = mEndX - mInitialX;
                    float deltaY = mEndY - mInitialY;
                    double angleInDegrees = Math.atan(deltaY / deltaX) * 180 / Math.PI;

                    mInitialX = mEndX;
                    mInitialY = mEndY;

                    mCurrTempIndicator.setRotation((float)angleInDegrees);
                    mCurrTempIndicator.setTranslationX((float)(310*(Math.cos(angleInDegrees))));
                    mCurrTempIndicator.setTranslationY((float)(310*(Math.sin(angleInDegrees))));




                    break;

                case MotionEvent.ACTION_UP:
                    allowRotating = true;
                    break;
            }



            return true;
        }

最佳答案

float dx = event.getX() - circleCenterX
float dy = event.getY() - circleCenterY;

// r is now the radius of the touch event, you can compare it with the radius of your circle to find out if it's close enough
float r = FloatMath.sqrt((dx * dx) + (dy * dy));

if(r > circleRadius - 10 && r < circleRadius + 10){
    // a is now the angle between the center point and the touch point in radians. With 0 being 3 o'clock, -/+PI being 9 o'clock -PI/2 at 12 o'clock and +PI/2 at 6 o'clock.
    float a = Math.atan2(dy, dx);
}

关于android - 如何查找 ACTION_MOVE 触摸事件是否在圆形路径上 : Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20543566/

相关文章:

安卓字符串颜色

java - 写入过程中无法从服务器读取

objective-c - 组合多个 y = mx + b 方程的最有效方法是什么?

javascript - -JS - 触摸-防止水平滚动

python - 无法使用 python-uinput 将触摸事件写入 uinput

android - 如何在 ListView 中的某些项目之后显示分隔符?

android - 在 inflated layout 中使用 setText() 到 TextView() 后不显示文本

c++ - 如何在C++逐位运算中计算最接近的8整数?

c# - 想知道为什么指针算法在 asp.net 中不起作用

objective-c - 从 TOP/BOTTOM 检测触摸