如何查找ACTION_MOVE触摸事件是否在循环路径上:Android

时间:2013-12-12 12:27:43

标签: android math touch

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

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

感谢。

更新:

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

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

有人可以看一下代码,让我知道我哪里出错了。

感谢。

@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;
        }

2 个答案:

答案 0 :(得分:1)

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);
}

答案 1 :(得分:0)

添加上述答案。我做了一些更改,使其正常工作。在这里,我正在使ImageView围绕一个固定的中心点旋转,这意味着ImageView将沿着圆周移动。

double centerX, centerY; //Center coordinates of the circle
int radius; //Set the radius in pixels

//The view which will revolve on the circumference of the circle
imageView = new ImageView(this);
imageView.setBackgroundResource(android.R.drawable.ic_menu_add); //Set your view icon
imageView.setPadding(10, 10, 10, 10);
relativeLayout.addView(imageView);

//Setting the imageView on the circumference of the circle
imageView.setTranslationX(centerX + radius);
imageView.setTranslationY(centerY);

//Add imageview to the parent layout dynamically(choice)
relativeLayout.addView(imageView);


imageView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

        switch (motionEvent.getAction()) {

            case MotionEvent.ACTION_MOVE:
            double dx = motionEvent.getRawX() - centerX;
            double dy = motionEvent.getRawY() - centerY; //Also subtract toolbar height and status bar height if applicable

            //Distance between user's point of touch and circle center
            double distance = Math.sqrt((dx * dx) + (dy * dy));

            if (distance > radius - 200 && distance < radius + 200) {
                double angle = Math.atan2(dy, dx);

                //Getting the point on the circumference of the circle using the angle and the radius
                double x = centerX + radius * Math.cos(angle);
                double y = centerY + radius * Math.sin(angle);

                //Finally move the view along the circimference of the circle as the user drags the view
                imageView.animate().y((float) y).x((float) x).setDuration(0).start();
            }
            break;
        }
        return true;
    }
});