我正在通过触摸事件围绕圆圈移动图像。 我希望用户触摸图像,当用户围绕圆圈拖动该图像时,它会移动,否则不会移动。
有人可以帮忙计算一下如何检查手指是否沿着圆圈移动并相应地移动图像。
谢谢。
更新:
我正在尝试围绕一个圆圈旋转图像。它已经放在圆边上。
但在触摸和移动 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/