sundar
sundar

Reputation: 13

Animation doesn't start even after calling from outside onCreate

I would like to do frame-by-frame animation in my android activity. I went through the developer guide and found that the start() method has to be called from outside the onCreate() method to get the animation started. I used the similar onTouchEvent() method to start the animation. But the animation is not started. While searching, I found the following post( Starting Frame-By-Frame Animation ) where it has been mentioned that animation could be started from onClick() method(But code was not provided). I am not able to do that. Could someone help me solve this problem?

One more important point I want to add is, I am able to call start() of Mediaplayer object from the onTouchEvent method and not the start() of animation.

I have one more question. I am using emulator 2.3.3. Is animation supported in SDK 2.3.3?

I am posting my code here:

public class firstface extends Activity {
MediaPlayer mp1;
AnimationDrawable anim;

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     

    mp1 = MediaPlayer.create(this, R.raw.red);
    status = ReadStatus();

    ImageView image = (ImageView)findViewById(R.id.image_1); 
    image.setBackgroundResource(R.drawable.anim_1);
    anim = (AnimationDrawable) image.getBackground();

    /*image.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
        mp1.start();            
        anim.start();
        } 
    });*/

    Button but_1_2 = (Button) findViewById(R.id.button_1_2);
    but_1_2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            to_be_writ = my_face + ":l:" + down + ":d:n"; 
            WriteStatus(to_be_writ);
            intent = "android.intent.action." + right + "FACE";
            startActivity(new Intent(intent));
        }
    });
}

public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mp1.start();
            anim.start();
            return true;
        }
        return super.onTouchEvent(event);
    }


    @Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    mp1.release();

}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    mp1.release();

}

Upvotes: 1

Views: 1299

Answers (1)

Levi
Levi

Reputation: 869

I had the same issue on 2.3.3 when trying to start() the animation from 2.3.3 in the onCreate method. I moved my call to start() to the onWindowFocusChanged(boolean) and it worked (as described here: http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html#start())

Upvotes: 2

Related Questions