0

I'm trying to do something very basic. I'm just trying to run an simple tween animation from a xml resource file for an Android application when a button is clicked. The animation will not start when I run the app. I'm going nuts trying to figure out why.

Here's the res/anim/spin.xml file:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http:schemas.android.com/apk/res/android"
    android:shareInterpolator="false">

    <rotate
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:toDegrees="360" />

</set>

Here's my activity class:

    package jorge.jorge.jorge;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.ImageView;

    public class Assignment5Activity extends Activity {
        /** Called when the activity is first created. */

        @Override

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);



            final Button btnSpin = (Button) findViewById(R.id.button1);
            btnSpin.setText("Start");
            btnSpin.setOnClickListener(new OnClickListener() {
                public void onClick(View v) 
                {


                    ImageView iv = (ImageView) findViewById(R.id.imageView1);
                    Animation an =    AnimationUtils.loadAnimation(Assignment5Activity.this, R.anim.spin);

                    iv.startAnimation(an);

                    if (an.hasStarted())
                    {
                        btnSpin.setText("Stop");
                    }
                    else
                    {
                        iv.startAnimation(an);
                    }
                }
            } ); 

        }

    }
rocklandcitizen
  • 979
  • 4
  • 17
  • 27

5 Answers5

0

Try this

final ImageView iv = (ImageView) findViewById(R.id.imageView1);
                    Animation an =        AnimationUtils.loadAnimation(Assignment5Activity.this, R.anim.spin);

 btnSpin.setOnClickListener(new OnClickListener() {
                public void onClick(View v) 
                {




                    iv.startAnimation(an);

                    if (an.hasStarted())
                    {
                        btnSpin.setText("Stop");
                    }
                    else
                    {
                        iv.startAnimation(an);
                    }
                }
            } ); 
0

Use a RotateAnimation, setting the pivot point to the centre of your image.

RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

// Start animating the image
final ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.startAnimation(anim);

// Later.. stop the animation
iv.setAnimation(null);
K_Anas
  • 31,226
  • 9
  • 68
  • 81
  • That didn't work either. I'm not sure why. Are you suppose to define the animation resource in the manifest xml file? – rocklandcitizen Jun 27 '12 at 19:03
  • @rocklandcitizen with this code you don't need to declare animation in xml!! if you want to work with animation in xml put it in your res/anim folder – K_Anas Jun 27 '12 at 21:47
  • The original code in my first post used the xml animation resource and it was located in the res/anim directory. I tried the code above without requesting the xml resource (just doing it programmatically as presented). For some reason that's beyond me it didn't work either. I'm thinking maybe I need to load the bitmap resource programmatically instead of loading it in my main.xml layout file. Althought that shouldn't matter in theory. I don't know. – rocklandcitizen Jun 28 '12 at 12:31
0

Solution:

spin.xml stays the same as your xml.
Run thiscode in your button's OnClickListener.

ImageView iv = (ImageView) findViewById(R.id.imageView1);
Animation an = AnimationUtils.loadAnimation(Assignment5Activity.this, R.anim.spin);
iv.startAnimation(an);
jenzz
  • 7,239
  • 6
  • 49
  • 69
Sanket
  • 499
  • 6
  • 8
0

Give the animation a duration. I believe by default it's 0 (as opposed to property animations which are ~300msec by default).

Additionally, you can use NineOldAndroids to backport the much superior ViewPropertyAnimator approach to pre-Honeycomb.

Delyan
  • 8,881
  • 4
  • 37
  • 42
0

Have you tried changing android:toDegrees from 360 to 359? To android, degree 0 is the same as 360 because number sequences begin at 0 not 1. So essentially, with the current setting at 360, telling it to go from degree 0 to degree 360 is like telling it to stay put.

    <rotate
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:toDegrees="359" />
Stephen
  • 1
  • 1