Rotate an Imagewith Animation

前端 未结 6 1650
死守一世寂寞
死守一世寂寞 2021-02-01 04:23

\"The

What I Have

I have an arrow image (like the left one). Whe

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 04:48

    Why dont you use RotateAnimation?

    create a folder named anim in res and a file named rotator.xml inside res/anim.

    
    

    Here one complete rotation will be completed in 4000ms (4 seconds). Now add a PNG image that you want to rotate into your drawable folder. Then open res/main.xml, after removing the default textView in the layout, add an ImageView and Button into the layout. Set the src property of the ImageView as your filename of the added image, for example android:src=”@drawable/myimg” Ok, lets edit the main class. In the onClick() for the button, add the necessary code for running the animation. Check the following code.

    public class AnimationActivity extends Activity {
    
        public ImageView  myImage ;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            myImage = (ImageView)findViewById(R.id.imageView1);
            final Animation myRotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotator);
            ((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener()    {
                @Override
                public void onClick(View arg0) {
                    myImage.startAnimation(myRotation);
                }
            });
        }
    }
    

提交回复
热议问题