2010-06-29 5 views
35

J'ai écrit ce qui suit pour faire tourner mon icône sur le centre de l'écran et à la place il tourne autour du coin supérieur gauche (c'est-à-dire, origine x = 0, y = 0 de l'ImageView). Il devrait être simple de définir certains attributs de ImageView ou RotateAnimation, mais je ne peux pas le comprendre.Comment faire pour faire tourner une icône android sur son point central?

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

Répondre

74

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

+1

Pouvez-vous expliquer cela. Alors que nous pouvons facilement modifier en fonction de nos besoins. – Nepster

+0

C'est ce que je cherche. – tounaobun

4

Cela fonctionne pour moi:

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

Comment ajouteriez-vous ceci à une image afin qu'elle tourne continuellement jusqu'à ce que vous lui disiez d'arrêter. – Zapnologica

+0

'rotationAnimation.setDuration (Animation.INFINITE);' et quand vous voulez arrêter l'animation: 'ImageView myRotatingImage = (ImageView) mRoot.findViewById (R.id.my_rotating_image)' ' myRotatingImage.clearAnimation() ; ' –

+3

Ce n'est pas de travail: java.lang.IllegalArgumentException: La durée de l'animation ne peut pas être négative –