user123456
user123456

Reputation: 265

Android rotate imageview animation issue

I am working android rotation of ImageView. When I run Project, animation in onCreate() its work fine, but when I try to start Animation on Click button its not working.

How can I fix it?

XML Code

<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" />

Java Class Code

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

}

}

Upvotes: 1

Views: 765

Answers (1)

Shivani Gupta
Shivani Gupta

Reputation: 271

Do the following steps and it will work definitely 1) give Id to your xml relativelayout (parent layout) [i have given just relative] 2) Do the below code :

 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();

    }
});

}

}

Upvotes: 2

Related Questions