44

I have written the following to spin my icon on the center of the screen and instead it rotates around the upper-left corner (i.e., origin x=0, y=0 of the ImageView). It should be simple to set some attributes of the ImageView or the RotateAnimation, but I can't figure it out.

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);
    }
}
Plinio.Santos
  • 1,709
  • 24
  • 31
mobibob
  • 8,670
  • 20
  • 82
  • 131

4 Answers4

86

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

Rich Schuler
  • 41,814
  • 6
  • 72
  • 59
6

This works for me:

img = (ImageView)findViewById(R.id.ImageView01);
RotateAnimation rotateAnimation = new RotateAnimation(30, 90,
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
eldarerathis
  • 35,455
  • 10
  • 90
  • 93
  • How would you add this to a image so that it continuously rotates until you tell it to stop. – Zapnologica Mar 13 '14 at 13:52
  • `rotationAnimation.setDuration(Animation.INFINITE);` and when you want to stop the animation: `ImageView myRotatingImage = (ImageView) mRoot.findViewById(R.id.my_rotating_image);` `myRotatingImage.clearAnimation();` – guy.gc Mar 29 '14 at 13:46
  • 3
    It's not work: java.lang.IllegalArgumentException: Animation duration cannot be negative – Omid Omidi Aug 10 '14 at 05:23
  • 2
    You can use - rotateAnimation.setRepeatCount(Animation.INFINITE); – AnkitRox Feb 06 '15 at 05:09
5

Here is the full example

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);
            }
        });
    }
}
Jules Dupont
  • 7,259
  • 7
  • 39
  • 39
Omar
  • 901
  • 11
  • 14
1
private fun showLoader() {
        binding.pbLoader.visibility = View.VISIBLE
        val ROTATE_FROM = 0.0f;
        val ROTATE_TO = 360.0f;
        val rotateAnimation = RotateAnimation(ROTATE_FROM, ROTATE_TO, Animation.RELATIVE_TO_SELF,
            0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        rotateAnimation.duration = 2.toLong() * 1500
        rotateAnimation.repeatCount = Animation.INFINITE
        binding.pbLoader.startAnimation(rotateAnimation)
    }


  
minato
  • 2,028
  • 1
  • 18
  • 30