Moving an Image in circular motion based on touch events in android

前端 未结 1 1811
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 08:40

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, ACTI

相关标签:
1条回答
  • 2020-12-16 09:03
    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))));
    
    0 讨论(0)
提交回复
热议问题