【问题标题】:how to rotate image around the circumference of a circle?如何围绕圆周旋转图像?
【发布时间】:2013-01-20 10:12:51
【问题描述】:

我是新手,所以不要怪我。我正在尝试开发一个可以制作音乐的 android 应用程序。我正在尝试制作一个在以圆形形式显示的一堆按钮上旋转的条形图,并且当它播放每个按钮所代表的声音时。但是到目前为止,我设法通过设置表示圆心的 x 和 y 坐标来使图像围绕屏幕中间旋转,但是当我尝试输入公式时 (x + radius*sin(angle)), (y + radius*cos(angle)),它只是移动我想在那个点旋转的图像。所以基本上我试图围绕由按钮或坐标定义的圆圈而不是实际的圆圈图像旋转图像。所以我需要围绕一个圆圈旋转一个图像或 imageView,而不仅仅是一个点。

我也添加了代码,所以你可以看看我做错了什么。

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

任何帮助将不胜感激!

【问题讨论】:

    标签: android rotation geometry rotateanimation


    【解决方案1】:

    代码如下:

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

    【讨论】:

      【解决方案2】:

      x^2 + y^2 = r^2

      参考:http://www.mathwarehouse.com/geometry/circle/equation-of-a-circle.php

      您应该围绕所有 (x,y) 为您选择的 r 值(圆的半径)满足该方程的对象的中心设置动画。

      我不是图形专家,所以请原谅我的回答过于简洁。

      【讨论】:

      • 我真的不认为我知道该怎么做.. 我尝试制作一个循环并使图像旋转 1 度并在每次循环更改 (360) 时移动位置但没有工作
      猜你喜欢
      • 2011-10-13
      • 2017-06-24
      • 1970-01-01
      • 2019-03-07
      • 2011-10-30
      • 1970-01-01
      • 2021-07-30
      • 2019-04-25
      • 2022-09-27
      相关资源
      最近更新 更多