2010-06-29 33 views
35

Tôi đã viết những điều sau đây để quay biểu tượng của tôi ở giữa màn hình và thay vào đó nó xoay quanh góc trên bên trái (tức là, xuất xứ x = 0, y = 0 của ImageView). Nó sẽ được đơn giản để thiết lập một số thuộc tính của ImageView hoặc RotateAnimation, nhưng tôi không thể tìm ra nó.Làm thế nào để quay một biểu tượng android trên điểm trung tâm của nó?

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

Trả lời

74

Hãy thử: r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

+1

Bạn có thể giải thích điều này. Để chúng tôi có thể dễ dàng sửa đổi theo nhu cầu của mình. – Nepster

+0

Đó là những gì tôi đang tìm kiếm. – tounaobun

4

này làm việc cho tôi:

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

Bạn sẽ thêm hình ảnh này vào hình ảnh để nó liên tục xoay cho đến khi bạn yêu cầu dừng lại. – Zapnologica

+0

'rotationAnimation.setDuration (Animation.INFINITE); ' và khi bạn muốn dừng hoạt hình: ' ImageView myRotatingImage = (ImageView) mRoot.findViewById (R.id.my_rotating_image);' 'myRotatingImage.clearAnimation() ; ' –

+3

Nó không hoạt động: java.lang.IllegalArgumentException: Thời lượng hoạt ảnh không được âm –

Các vấn đề liên quan