在移动圆周运动图像基于android的触摸事件 [英] Moving an Image in circular motion based on touch events in android

查看:178
本文介绍了在移动圆周运动图像基于android的触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图移动一个ImageView的(不旋转)。的运动被认为是在一个圆的边缘。这个圆也是一个图像图。

基于对onTouch,ACTION_MOVE事件

,我想移动它。

诺埃的姿态的尴尬是,使用可以不移动手指在一个完美的循环方式,但我想,以确保图像依然围绕这个圈子的边缘移动。

我目前使用以下内部ACTION_MOVE:

  mCurrTempIndicator.setTranslationX(event.getX());
mCurrTempIndicator.setTranslationY(event.getY());
 

但是,这将不会在一个完美的圆移动。

可能有人请帮忙。

更新:code

  @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){

            开关(event.getAction()){

                案例MotionEvent.ACTION_DOWN:


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

                    打破;

                案例MotionEvent.ACTION_MOVE:

                    mEndX = event.getX();
                    门迪= event.getY();

                    浮DELTAX = mEndX  -  mInitialX;
                    漂浮移动deltaY =门迪 -  mInitialY;
                    双angleInDegrees = Math.atan(移动deltaY / DELTAX)* 180 / Math.PI;

                    mInitialX = mEndX;
                    mInitialY =门迪;

                    mCurrTempIndicator.setRotation((浮点)angleInDegrees);
                    mCurrTempIndicator.setTranslationX((浮动)(310 *(Math.cos(angleInDegrees))));
                    mCurrTempIndicator.setTranslationY((浮动)(310 *(Math.sin(angleInDegrees))));




                    打破;

                案例MotionEvent.ACTION_UP:
                    allowRotating = TRUE;
                    打破;
            }



            返回true;
        }
 

解决方案
  1. 在计算圆的中心点
  2. 获取当前触摸点
  3. 计算中心和新的触摸点之间的夹角
  4. 使用角度和圆弧半径(X = R * COS(角度),Y = R *罪(角度))计算圆上的点。
  5. 复位图像位置到新点。

要获取的角度用下面的公式

 移动deltaY = P2_y  -  P1_y
DELTAX = P2_x  -  P1_x
angleInDegrees =反正切(移动deltaY / DELTAX)* 180 / PI

// $ C $内ACTION_MOVE情况c
mInitialX = event.getX();
mInitialY = event.getY();
浮DELTAX = circleCenter.x  -  mInitialX;
漂浮移动deltaY = circleCenter.y  -  mInitialY;
双angleInRadian = Math.atan2(yDiff,xDiff);
的PointF pointOnCircle =新的PointF();
pointOnCircle.x = circleCenter.x +((浮动)(circleRadius *(Math.cos(angleInRadian))));
pointOnCircle.y = circleCenter.y +((浮动)(circleRadius *(Math.cos(angleInRadian))));
 

I am trying to move an ImageView (not rotate). The movement is supposed to be on the edge of a circle. This circle is also an image view.

based on the onTouch, ACTION_MOVE event, I am trying to move it.

Noe the Dilema is that the use may not move the finger in a perfectly circular fashion but I would like to make sure that the image still moves around edge of this circle.

I am currently using the following inside ACTION_MOVE:

mCurrTempIndicator.setTranslationX(event.getX());
mCurrTempIndicator.setTranslationY(event.getY());

But this will not move in a perfect circle.

could someone please help.

UPDATE: code

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

解决方案

  1. calculate the center Point of the Circle
  2. get the current touch point
  3. calculate the angle between center and new touch point
  4. Calculate the point on the circle using angle and radius of circle (x = r * cos(angle), y = r * sin(angle)).
  5. Reset the image position to the new point.

To get the angle use the below equation

deltaY = P2_y - P1_y
deltaX = P2_x - P1_x
angleInDegrees = arctan(deltaY / deltaX) * 180 / PI

//Code inside ACTION_MOVE case
mInitialX = event.getX();
mInitialY = event.getY();
float deltaX = circleCenter.x - mInitialX;
float deltaY = circleCenter.y - mInitialY;
double angleInRadian = Math.atan2(yDiff, xDiff);
PointF pointOnCircle = new PointF();
pointOnCircle.x = circleCenter.x + ((float)(circleRadius*(Math.cos(angleInRadian))));
pointOnCircle.y = circleCenter.y + ((float)(circleRadius*(Math.cos(angleInRadian))));

这篇关于在移动圆周运动图像基于android的触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆