Search code examples
androidrotationgeometryrotateanimation

how to rotate image around the circumference of a circle?


I'm new to this so don't blame me. I am trying to develop an android app that would make music. I am trying to make a bar that rotates over a bunch of buttons that are displayed in the form of a circle, and when it does to play the sound represented by every button. However so far I managed to make an image rotate around the middle of the screen by setting x and y coordinates representing the centre of the circle, but when I try to put the formula (x + radius*sin(angle)), (y + radius*cos(angle)), it just moves the image I want to rotate at that point. So basically I am trying to rotate an Image around an circle defined by buttons or coordinates rather then an actual circle image. So I need to rotate an image or imageView around a circle, not just a point.

I have added the code ass well so you could have a look at what I'm doing wrong.

ImageView bara = (ImageView) findViewById(R.id.floating_image);

layoutParams[9] = new RelativeLayout.LayoutParams

(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        toop = Math.round(size.x/2);      // + 90*Math.sin(ANGLE)); 
        lefft = Math.round(size.y/2);    // + 90*Math.cos(ANGLE)); 
        top = (int) toop;
        left = (int) lefft;
        layoutParams[9].setMargins(top, left, 0, 0);
        bara.setLayoutParams(layoutParams[9]);
        RotateAnimation rAnim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF,  0 , Animation.RELATIVE_TO_SELF, 0);
        rAnim.setRepeatCount(Animation.INFINITE);
        rAnim.setInterpolator(new LinearInterpolator());
        rAnim.setDuration(8000);
        bara.startAnimation(rAnim);

Any help would be really appreciated !!


Solution

  • the code looks like :

        private float mCalcX;//x-coord of object
        private float mCalcY;//y-coord of object
        private double mCenterX;//x-coord of center of circle
        private double mCenterY;//y-coord of center of circle
        private double mRadius;//circle radius
        private double mAngleRadians;//angle of your object to draw in RADs
    
        // whenever you draw the object, calculate the new X and Y coords
        mCalcX = (float) (mCenterX+(mRadius*Math.cos(mAngleRadians)));
        mCalcY = (float) (mCenterY+(mRadius*Math.sin(mAngleRadians)));
    
        public void setRadius(double r)
        {
            mRadius = r;
        }
    
        public void setStartingAngle(double radians)
        {
            mAngleRadians = radians;
        }
    
        public void setRotationSpeed(double radians)
        {
            mRotationSpeed = radians;
        }
    
        public void increaseRotationAngle()
        {
            mAngleRadians += mRotationSpeed;
        }
    
        public void decreaseRotationAngle()
        {
            mAngleRadians -= mRotationSpeed;
        }