问题描述
我正在通过触摸事件围绕一个圆圈移动图像.我希望用户触摸图像,并且当用户将其拖动到线圈周围时,它会移动,否则它不会移动.
I am moving an image around a circle via touch events.I want the user to touch the image and when the user drags this image around the cirle, it moves, otherwise it doesn't move.
有人可以在数学上提供帮助,如何检查手指是否沿圆圈移动,然后他们相应地移动图像.
Could someone please help with the math on how to check if the finger in moving along the circle or not and them move the image accordingly.
谢谢.
更新:
我正在尝试将图像绕圆旋转.它已经放置在圆形边缘上.
I am trying to revolve an image around a circle. It is already placed on the circle edge.
但是在触摸和移动动作时,它以自身为中心,然后开始围绕定义的半径移动.
But on touch and move actions it takes it self as the centre and then starts moving around a defined radius.
有人可以看一下代码,让我知道我要去哪里了.
could someone pls see the code and let me know where i am going wrong.
谢谢.
@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);
}
这篇关于如何查找ACTION_MOVE触摸事件是否在循环路径上:Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!