2010-06-29 49 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