Android旋转imageview动画问题

Android rotate imageview animation issue

我正在 android 旋转 ImageView。当我 运行 项目时,onCreate() 中的动画工作正常,但是当我尝试在单击按钮上启动动画时它不起作用。

我该如何解决?

XML代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/getAngle"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:inputType="number" />

<ImageView
    android:id="@+id/rotateImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/spinner_new" />

<Button
    android:id="@+id/startbutton"
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Start" />

JavaClass代码

public class MainActivity extends Activity {

EditText getAngle;

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


    getAngle = (EditText) findViewById(R.id.getAngle);
    Button startbutton = (Button) findViewById(R.id.startbutton);
    startbutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String endPointString = getAngle.getText().toString();
            int endPointInt = Integer.parseInt(endPointString);
            ImageView rotateImage = (ImageView) findViewById(R.id.rotateImage);
            Animation rotateanimation = new RotateAnimation(0, endPointInt,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            rotateanimation.setDuration(1000);
            rotateanimation.setRepeatCount(0);
            rotateanimation.setRepeatMode(Animation.REVERSE);
            rotateanimation.setFillAfter(true);
            rotateImage.setAnimation(rotateanimation);
        }
    });

}

}

按照下面的步骤,一定可以的 1)给你的xml relativelayout(父布局)[我只给了相对] 2) 执行以下代码:

 public class MainActivity extends Activity {

EditText getAngle;
ImageView rotateImage;
RelativeLayout relative;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


getAngle = (EditText) findViewById(R.id.getAngle);
rotateImage = (ImageView) findViewById(R.id.rotateImage);
  relative = (RelativeLayout)findViewById(R.id.relative);
Button startbutton = (Button) findViewById(R.id.startbutton);
startbutton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        String endPointString = getAngle.getText().toString();
        int endPointInt = Integer.parseInt(endPointString);

        Animation rotateanimation = new RotateAnimation(0, endPointInt,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        rotateanimation.setDuration(1000);
        rotateanimation.setRepeatCount(0);
        rotateanimation.setRepeatMode(Animation.REVERSE);
        rotateanimation.setFillAfter(true);
        rotateImage.setAnimation(rotateanimation);
        rotateanimation.start();
         relative.invalidate();

    }
});

}

}