2010-06-29 70 views
35

我已经写了下面的代码来让我的图标旋转到屏幕的中心,而是围绕左上角(即ImageView的原点x = 0,y = 0)旋转。设置ImageView或RotateAnimation的某些属性应该很简单,但我无法弄清楚。如何在中心点上旋转一个android图标?

public class IconPromoActivity extends Activity { 
    private static final float ROTATE_FROM = 0.0f; 
    private static final float ROTATE_TO = -10.0f * 360.0f;// 3.141592654f * 32.0f; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ImageView favicon = (ImageView) findViewById(R.id.favicon); 

     RotateAnimation r; // = new RotateAnimation(ROTATE_FROM, ROTATE_TO); 
     r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, 0, 0, 40, 0); 
     r.setDuration((long) 2*1500); 
     r.setRepeatCount(0); 
     favicon.startAnimation(r); 
    } 
} 

回答

74

尝试: r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

+1

你能解释这一点。这样我们可以根据我们的需要轻松修改。 – Nepster 2015-05-05 07:52:33

+0

这就是我要找的。 – tounaobun 2015-06-04 09:39:33

4

这个工作对我来说:

img = (ImageView)findViewById(R.id.ImageView01); 
RotateAnimation rotateAnimation = new RotateAnimation(30, 90, 
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
+0

你会如何将它添加到图像,以便它不断旋转,直到你告诉它停止。 – Zapnologica 2014-03-13 13:52:36

+0

'rotationAnimation.setDuration(Animation.INFINITE);' ,当你想停止动画: 'ImageView的myRotatingImage =(ImageView的)mRoot.findViewById(R.id.my_rotating_image);'' myRotatingImage.clearAnimation() ;' – 2014-03-29 13:46:28

+3

这不是工作:java.lang.IllegalArgumentException:动画持续时间不能为负 – 2014-08-10 05:23:26