web-dev-qa-db-fra.com

Comment faire tourner une icône Android sur son point central?

J'ai écrit ce qui suit pour faire tourner mon icône au centre de l'écran et à la place, elle tourne autour du coin supérieur gauche (c'est-à-dire Origin 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.

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

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

82
Rich Schuler

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);
5
Rodrigo Ferreira

Voici l'exemple complet

public class MainActivity extends AppCompatActivity implements MainActivityMvpModel {
    ImageView imageViewThumb;

    private static final float ROTATE_FROM = 30.0f;
    private static final float ROTATE_TO = 360.0f;
    RotateAnimation r; // = new RotateAnimation(ROTATE_FROM, ROTATE_TO);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonTest= (Button) findViewById(R.id.button_test);

        imageViewThumb= (ImageView) findViewById(R.id.icon_thumb);
        imageViewThumb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                r = new RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                r.setDuration((long) 2*500);
                r.setRepeatCount(0);
                imageViewThumb.startAnimation(r);

                imageViewThumb.setColorFilter(R.color.colorThumbPressed);
            }
        });
    }
}
4
Omar Hasan