Android – 以圆圈forms移动对象

我遇到了麻烦,我需要制作一个物体(乒乓球拍)才能沿着屏幕的圆形路线移动。 同样的事情就好像你有一个恒定的y轴值,它只会沿着x轴移动,因为你将手指拖到它上面,但是要将它限制在一个圆形路线上。

任何见解? 我看到了这个东西http://www.kirupa.com/developer/mx/circular.htm

它只有助于弄清楚如何在一个圆圈上不停地移动某些东西(虽然它是Flash,但想法是一样的)

谢谢

这是我用一个手指旋转一个imageview的代码块。

private float mCenterX, mCenterY; private float direction = 0; private float sX, sY; private float startDirection=0; private void touchStart(float x, float y) { mCenterX = this.getWidth() / 2; mCenterY = this.getHeight() / 2; sX = x; sY = y; } private void touchMove(float x, float y) { // this calculate the angle the image rotate float angle = (float) angleBetween2Lines(mCenterX, mCenterY, sX, sY, x, y); direction = (float) Math.toDegrees(angle) * -1 + startDirection; this.invalidate(); } @Override protected void onDraw(Canvas canvas) { canvas.rotate(direction, mCenterX, mCenterY); super.onDraw(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // record the start position of finger touchStart(x, y); break; case MotionEvent.ACTION_MOVE: // update image angle touchMove(x, y); break; case MotionEvent.ACTION_UP: startDirection = direction; break; } return true; } public double angleBetween2Lines(float centerX, float centerY, float x1, float y1, float x2, float y2) { double angle1 = Math.atan2(y1 - centerY, x1 - centerX); double angle2 = Math.atan2(y2 - centerY, x2 - centerX); return angle1 - angle2; } 

希望这有帮助

编辑:基本上,上面的代码所做的是使用户能够旋转图像,并且图像的中心不会改变。 angleBetween2Line()用于计算手指在中心为图像中心的圆圈中移动的程度。

圆上的点可以由函数定义:

 x = a + r cos(θ) y = b + r sin(θ) 

其中(a,b)是圆的中心。

根据您所希望的速度,您可以说每T秒需要一个完整的圆圈。 如果t是动画开始以来的时间:

 θ = (360 / T) * (t % T) 

您可以使用这些函数创建自己的ViewAnimation,OpenGL函数,或者如果您使用canvas,则可以在onDraw()事件期间设置paddle的位置。