How to spin an android icon on its center point?

前端 未结 3 1041
野趣味
野趣味 2020-12-04 23:45

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 sh

相关标签:
3条回答
  • 2020-12-05 00:19

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

    0 讨论(0)
  • 2020-12-05 00:32

    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);
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-05 00:34

    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);
    
    0 讨论(0)
提交回复
热议问题