views:

30

answers:

2

What's best practice for structuring an activity, which should begin by displaying an animated image, and then move to another "activity" by intent when the animation is complete? I understand some of the basic principles, but I am getting lost on structuring the flow here.

public class Scanimation extends Activity  { 

 //create name of animation 
Animation myFadeInAnimation;

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.scanning_view);

//grab the imageview from the layout, and then load the animation listener
      ImageView myImageView = (ImageView) findViewById(R.id.blinkingView01);
      AnimationListener al= new AnimationListener() {

    public void onAnimationStart(Animation animation) {
    // do nothing       
    }

    public void onAnimationRepeat(Animation animation) {
    // do nothing       
    }

        // at the end of the animation, start new activity
    public void onAnimationEnd(Animation animation) {
        Intent myIntent = new Intent (view.getContext(), LastActivity.class);
         startActivity(myIntent);               
    }

     // get the animation effects which are in the XML file and defines how to animate it       Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in);

};

     //okay now start the animation on screen
     myImageView.startAnimation(myFadeInAnimation); 

    // go get the vibration service and vibrate quickly while screen is animated           
    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long milliseconds = 2000;
v.vibrate(milliseconds);
long[] pattern = { 500, 300 };
v.vibrate(pattern, -1);

      }

   }

Most of the tutorials I've read appear to use this type of flow. I'm pulling in my main layout and view, then animating the specific view as well as vibrating the device for a quick second. At the end of the animation, the intent to start my next or last activity does not fire.

What am I missing here?

A: 

I do not see where you are calling setAnimationListener() to associate al with myFadeInAnimation. Of course, I don't see where you are actually creating myFadeInAnimation, either.

CommonsWare
Thank you for the fresh look, this has been quite an exercise in frustration for me. My eyes are shot from looking at this for hours, I'll take another stab at it tomorrow and examine the two things you mentioned.
Keith
CommonsWare, thank you for pointing out what I had missed. I spent about 15 minutes restructuring the code, and paid attention to your observations. I was able to create the code and get it working.
Keith
Thought I should post the new version in case somebody else wants to see it. Now I just need to figure out how to restart it after a pause (i.e., exit to take a call, and come back)
Keith
A: 

Here's the corrected version:

public class Scanimation extends Activity implements     AnimationListener{ 

 public Animation myFadeInAnimation;

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.scanning_view);

    ImageView myImageView = (ImageView) findViewById(R.id.blinkingView01);
    myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in);   

    myFadeInAnimation.setAnimationListener(this);
    myImageView.startAnimation(myFadeInAnimation);
}

        public void onAnimationEnd(Animation animation) {
            Intent myIntent = new Intent (this, Lastactivity.class);
            startActivity (myIntent);                       
     } 

        public void onAnimationRepeat(Animation animation) { 

     } 

        public void onAnimationStart(Animation animation) { 
            // go get the vibrator service         
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

            // 1. Vibrate for 2000 milliseconds
            long milliseconds = 2000;
            v.vibrate(milliseconds);

            // 2. Vibrate in a Pattern 
            long[] pattern = { 500, 300 };
            v.vibrate(pattern, -1);
     } 
}
Keith